0

我正在尝试在 WordPress 页面上创建一个表单,该表单允许我将数据输入到 mysql 表中。当我输入代码时,主键会自动增加,但是数据本身不会进入数据库。这是mysql中的表信息:

    CREATE TABLE `macscabs`.`customers` (
`custno` INT NOT NULL AUTO_INCREMENT ,
`sname` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`fname` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`homeaddr` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`commondest` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`mobileno` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`homeno` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`email` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
PRIMARY KEY ( `custno` ) 
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;     

以下是来自 WordPress 页面的代码与 php 在同一页面上:

<table>
<form name="form1" method="post" action="<?php echo $_SERVER[PHP_SELF] ?>">
<strong>Please enter your information in order to download the Macs Cabs App</strong>
<tr><td>
Surmane Name:</td><td><input name="sname" type="text" id="sname"></td></tr>
<tr><td>
First Name:</td><td><input name="fname" type="text" id="fname"></td></tr>
<tr><td>
Home Address:</td><td><input name="homeaddr" type="text" id="homeaddr"></td></tr>
<tr><td>
Most common Destination:</td><td><input name="commondest" type="text" id="commondest"></td></tr>
<tr><td>
Mobile Number:</td><td><input name="mobileno" type="text" id="mobileno"></td></tr>
<tr><td>
Home Number:</td><td><input name="homeno" type="text" id="homeno"></td></tr>
<td>
Email Address:</td><td><input name="email" type="text" id="email"></td></tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("macscabs", $con);

$sql="INSERT INTO `customers` (`sname`, `fname`, `homeaddr`, `commondest`, `mobileno`,       `homeno`, `email`)  
VALUES    ('{$_POST['sname']}','{$_POST['fname']}','{$_POST['homeaddr']}','{$_POST['commondest']}','{$_POST['m    obileno']}',
'{$_POST['homeno']}','{$_POST['email']}')";


 if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
 echo "1 record added";

$info = mysql_info(); echo $info;

mysql_close($con);
?> 

我还通过输入下面的代码手动插入数据而不使用表单来测试数据库和网页之间的连接,它可以工作。但是,当我尝试使用表单插入它时,我无法让它工作。

mysql_query("INSERT INTO customers (sname, fname, homeaddr, commondest, mobileno, homeno, email)
VALUES ('mcguinness','dave','moate','athlone','0254','56587',5)");
4

4 回答 4

0

值(“'.$_post['EACH_NAME'].'”,“'.$_post['Next_Name_etc'].'”);

于 2012-11-10T15:10:01.707 回答
0

当然,它会在没有输入数据的情况下添加到 mysql 表中,因为您没有指示单击提交按钮的条件,因此这意味着一旦加载页面,插入查询也正在执行。你为什么不试试这个

<?php
 if(isset($_POST['Submit'])){ // if the submit button is clicked

 $con = mysql_connect("localhost","root","");
 if (!$con)
 {
   die('Could not connect: ' . mysql_error());
 }

 mysql_select_db("macscabs", $con);

  //using @musicvicious insert query
  $sql="INSERT INTO `customers` (`sname`, `fname`, `homeaddr`, `commondest`, `mobileno`, `homeno`, `email`)  VALUES ('".$_POST['sname']."', '".$_POST['fname']."', '".$_POST['homeaddr']."', '".$_POST['commondest']."', '".$_POST['mobileno']."', '".$_POST['homeno']."', '".$_POST['email']."')";


  if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  echo "1 record added";

  $info = mysql_info(); echo $info;

  mysql_close($con);

  }//end condition bracket
 ?> 

但最好的方法是使用 $wpdb->query(); 检查这个wpdb

于 2012-11-11T06:05:11.150 回答
0

尝试这个:

$sql="INSERT INTO `customers` (`sname`, `fname`, `homeaddr`, `commondest`, `mobileno`, `homeno`, `email`)  
VALUES ('".$_POST['sname']."', '".$_POST['fname']."', '".$_POST['homeaddr']."', '".$_POST['commondest']."', '".$_POST['mobileno']."', '".$_POST['homeno']."', '".$_POST['email']."')";
于 2012-11-10T15:01:36.237 回答
0
this will do
<?php
        if (!empty($_POST)) {
        global $wpdb;
            $table = wp_achord;
            $data = array(
                'name' => $_POST['yourname'],
                'chord'    => $_POST['chord']
            );
            $format = array(
                '%s',
                '%s'
            );
            $success=$wpdb->insert( $table, $data, $format );
            if($success){
            echo 'data has been save' ; 
}
}
else   {
?>
        <form method="post">
        <input type="text" name="yourname">
        <textarea name="chord"></textarea>
        <input type="submit">
        </form>

       <?php }  ?>
于 2013-07-30T17:01:06.967 回答