为什么我的php表单无法提交到我的sql数据库

时间:2016-02-12 09:40:07

标签: php jquery mysql wordpress forms



<html>
   
   <head>
      <title>Add a New Open Mic</title>

   </head>
   
   <body>
      <?php
         if(isset($_POST['add'])) {
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "mlcarelo_wrdp2";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
            }
            
            if(! get_magic_quotes_gpc() ) {
			$State= addslashes ($_POST['State']);
			$City= addslashes ($_POST['City']);
			$Place= addslashes ($_POST['Place']);
			$Address= addslashes ($_POST['Address']);
			$Day= addslashes ($_POST['Day']);
			$Time= addslashes ($_POST['Time']);
			$Host= addslashes ($_POST['Host']);
			$Contact= addslashes ($_POST['Contact Information']);

            }else {
			$State= addslashes ($_POST['State']);
			$City= addslashes ($_POST['City']);
			$Place= addslashes ($_POST['Place']);
			$Address= addslashes ($_POST['Address']);
			$Day= addslashes ($_POST['Day']);
			$Time= addslashes ($_POST['Time']);
			$Host= addslashes ($_POST['Host']);
			$Contact= addslashes ($_POST['Contact Information']);
            }
            
            
            
            $sql = "INSERT INTO 'responses' (State,City, Place, 
               Address, Day, Time, Host, Contact Information)VALUES('$State','$City','$Place', '$Address', '$Day', '$Time', '$Host', '$Contact');
               
            mysql_select_db('mlcarelo_wrdp2');
            $retval = mysql_query( $sql, $conn );
            
            if(! $retval ) {
               die('Could not enter data: ' . mysql_error());
            }
            
            echo "Entered data successfully\n";
            
            mysql_close($conn);
         }else {
           
            }
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border = "0" cellspacing = "1" 
                     cellpadding = "2">
                  
                     <tr>
                        <td width = "100">State</td>
                        <td><input name = "State" type = "text" 
                           id = "State"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">City</td>
                        <td><input name = "City" type = "text" 
                           id = "City"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Place</td>
                        <td><input name = "Place" type = "text" 
                           id = "Place"></td>
                     </tr>
                  <tr>
                        <td width = "100">Address</td>
                        <td><input name = "Address" type = "text" 
                           id = "Address"></td>
                     </tr>
					 <tr>
                        <td width = "100">Day</td>
                        <td><input name = "Day" type = "text" 
                           id = "Day"></td>
                     </tr>
					 <tr>
                        <td width = "100">Time</td>
                        <td><input name = "Time" type = "text" 
                           id = "Time"></td>
                     </tr>
					 <tr>
                        <td width = "100">Host</td>
                        <td><input name = "Host" type = "text" 
                           id = "Host"></td>
                     </tr>
					 <tr>
                        <td width = "100">Contact Information</td>
                        <td><input name = "Contact Information" type = "text" 
                           id = "Contact Information"></td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                  
                    
                  
                  </table>
               </form>
            
            <?
   
   </body>
</html>
&#13;
&#13;
&#13;

如何让我的表单工作,以便用户可以输入信息并提交进入sql数据库?我不太了解Form = action。我正在使用wordpress。我已经成功地从我的数据库中拉出表格,所以我知道我的PHP编码开始是正确的,查询应该是正确的。

1 个答案:

答案 0 :(得分:0)

您正在混合mysql_mysqli_。仅使用mysqli_

$sql = "INSERT INTO responses (State, City, Place, Address, Day, Time, Host, ContactInformation) VALUES ('$State', '$City', '$Place', '$Address', '$Day', '$Time', '$Host', '$Contact')";
$retval = mysqli_query($conn, $sql);
if(! $retval ) {
    die('Could not enter data: ' . mysqli_error());
}
echo "Entered data successfully\n";
mysqli_close($conn);

您的查询中也存在多个语法错误,例如:表名不包含在' '中。请注意,您不需要使用mysqli_select_db,您已在连接字符串中执行此操作。我确定你的专栏Contact Information在你的表格中没有空格,所以你也需要纠正这个问题。我使用过ContactInformation,请确保这是正确的。

旁注:

以这种方式插入数据对sql-injection非常开放。请改用预备语句!!