简体   繁体   中英

PHP/HTML Form Feeding Blank Entries to MySQL

I am a PHP/MySQL Greenback here, and I have run into an issue with a simple form I am trying to feed into a MySQL database via PHP, that keeps feeding blank entries.

The form is live, connecting and feeding to the DB, however whenever I submit an entry, my confirmation echo's back that it Connected successfullyINSERT INTO db_name.events (eventname, eventprice, eventabout) VALUES ('', '', '') Works! even though the values were populated in the HTML form. Then when I log in and check the MySQL Database through PHPmyadmin I can see that it indeed created a new row in the table, but it is blank.

I have spent hours combing the syntax line by line and can't seem to find anything out of place and I have now added a bunch of troubleshooting steps in to try and solve it.

Any help is greatly appreciated!

The HTML form is as follows:

<form method="post" action="eventtestconnect.php"><table style="border: 0; margin-left: auto; margin-right:auto;text-align: left">
      <tr>
        <td>Event Name:</td>
        <td><input name="name"></td>
      </tr> 
      <tr>
        <td>Event Price:</td>
        <td><input name="price"></td>
      </tr>
      <tr>
        <td>Event Description:</td>
        <td><textarea name="description" cols="40" rows="5">
        </textarea></td>
      </tr>
   </table>
    <br><br>
    <input type="submit" value="Submit">
  </form>

And the PHP file that connects to this form is:

<?php

// connect to database
$dbhost = '111.111.11.111';
$dbuser = 'db_name';
$dbpass = 'pwpwpw';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

//select database
mysql_select_db("db_name", $conn);
if ($_POST)
{

// scrub inputs
$name = mysql_real_escape_string($conn, $_POST['name']); 
$price = mysql_real_escape_string($conn, $_POST['price']);
$description = mysql_real_escape_string($conn, $_POST['description']);

// prepare query
$sql = "INSERT INTO db_name.events (eventname, eventprice, eventabout)
 VALUES ('$name', '$price', '$description')"; 

// execute query
mysql_query($sql);

// close connection
mysql_close($conn);
echo $sql;
}
?>

Thanks in advance for any help, I have been browsing these forums grabbing help and tips. Seems like a great community!

You passing in the arguments to mysql_real_escape_string in the wrong order. It should be:

$name = mysql_real_escape_string($_POST['name'], $conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM