简体   繁体   中英

PHP, MYSQL, phpmyadmin - Using a html form to input records to a database

My objective is to have two separate php pages.One page is for a user to input data into form fields (first name, last name, product, size), and the other is to display the updated table (after the user submits their fields). So far, I have created the two pages, with a functional form and a page that displays my tables results. Here is my code:

Form.php

<form action="process.php" method="post">
<p>
First Name:<input type="text" name="first_name"><br>
Last Name:<input type="text" name="last_name"><br>
Product:<input type="text" name="product"><br>
Size:<input type="text" name="size"><br>
</p>
<input type="submit" name="submit">
</form>

<?php
if (isset($POST['submit'])) {
    $con = mysql_connect("localhost","username","password");
if (!$con){
die("Connection was not successful:" . mysql_error());      
}
mysql_select_db("database_name",$con);

$sql = "INSERT INTO Order_1 (First_Name, Last_Name, Product, Size) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[product]','$_POST[size]')";


$myData = mysql_query($sql,$con);

mysql_close($con);
}
?>

And here is my results.php page, which displays my table:

<?php   

$con = mysql_connect("localhost","username","password");
if (!$con){
die("Connection was not successful:" . mysql_error());      
}
mysql_select_db("database name",$con);
$sql = "SELECT * FROM Order_1";
$myData = mysql_query($sql,$con);
echo "<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Product</th>
<th>Size</th>
</tr>";

while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['First Name'] . "</td>"; 
echo "<td>" . $record['Last Name'] . "</td>";
echo "<td>" . $record['Product'] . "</td>";
echo "<td>" . $record['Size'] . "</td>";
echo "</tr>";
}
echo"</table>";
mysql_close($con);

?>

I currently have two records in my table, that I manually typed into phpmyadmin. When i fill out this form, and click submit, it takes me to the "process.php" but nothing has changed. It's still only displaying the two records I have. And inside my PHPmyAdmin, it only has those two records. So my form isn't working how i'd like -- inputting a record submitted from the form, from a user, and displaying the entire table, included the record just recorded from the user, on the "process.php" code. I have gone over my code for (oh my god its been 4 hours now) and I can't find the solution. I've tested numerous things. Can anyone help? It'd be much appreciated.

$sql = "INSERT INTO Order_1 (First_Name, Last_Name, Product, Size) VALUES ('".$_POST[first_name]."','".$_POST[last_name]."','".$_POST[product]."','".$_POST[size]."')";

您忘记在查询中串联变量,这就是为什么它不插入的原因。

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