数据不会通过WordPress表单使用php进入mysql数据库

时间:2012-11-10 14:46:43

标签: php mysql wordpress

我正在尝试在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 个答案:

答案 0 :(得分: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']."')";

答案 1 :(得分:0)

VALUES(“'。$ _ post ['EACH_NAME']。'”,“'。$ _ post ['Next_Name_etc']。'”);

答案 2 :(得分: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-&gt; query();请检查此wpdb

答案 3 :(得分: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 }  ?>