When I run this code, it displays the details from the existing DB but does not update the DB with the entries that are put into the form.
<html>
<body>
<center>
<h2>Insert Product Details</h2>
<hr />
</center>
<?php
include('db_conn.php'); //db connection
if($result = $con->query("SELECT * FROM dbName")){
if($result->num_rows){
$rows = $result->num_rows;
if(isset($_POST['updated'])){
$qry = "INSERT INTO dbName (ID, Name, Price, Description)
VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";
}
echo '<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
';
while($row = $result->fetch_object()) {
echo '<tr>',
'<td>', $row->ID, '</td>',
'<td>', $row->Name, '</td>',
'<td>', $row->Price, '</td>',
'<td>', $row->Description, '</td> </tr>
';
}
echo '
<form method="POST" >
</table>
<hr />
<table>
<tr>
<td>ID:</td>
<td>
<input type = "text"
name = "inID"
value = " "
size = "3">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type = "text"
name = "inName"
value = " "
size = "30">
</td>
</tr>
<tr>
<td>Price:</td>
<td>
<input type = "text"
name = "inPrice"
value = " "
size = "7">
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type = "text"
name = "inDescription"
value = " "
size = "60">
</td>
</tr>
</table>
<input type = "submit" value = "insert" name="updated">
</form>
';
}
}
/* close connection */
$con->close();
?>
</body>
However, I have tried a simple echo in place of the insert query and it runs as I would expect...nothing before the submit button is pressed and then "Name entered: thename once the form has been submitted.
<html>
<body>
<center>
<h2>Insert Product Details</h2>
<hr />
</center>
<?php
include('db_conn.php'); //db connection
if($result = $con->query("SELECT * FROM dbName")){
if($result->num_rows){
$rows = $result->num_rows;
if(isset($_POST['updated'])){
echo "Name entered: " , $_POST[inName];
}
echo '<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
';
while($row = $result->fetch_object()) {
echo '<tr>',
'<td>', $row->ID, '</td>',
'<td>', $row->Name, '</td>',
'<td>', $row->Price, '</td>',
'<td>', $row->Description, '</td> </tr>
';
}
echo '
<form method="POST" >
</table>
<hr />
<table>
<tr>
<td>ID:</td>
<td>
<input type = "text"
name = "inID"
value = " "
size = "3">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type = "text"
name = "inName"
value = " "
size = "30">
</td>
</tr>
<tr>
<td>Price:</td>
<td>
<input type = "text"
name = "inPrice"
value = " "
size = "7">
</td>
</tr>
<tr>
<td>Description:</td>
<td>
<input type = "text"
name = "inDescription"
value = " "
size = "60">
</td>
</tr>
</table>
<input type = "submit" value = "insert" name="updated">
</form>
';
}
}
/* close connection */
$con->close();
?>
</body>
I have also tested the replaced query in another form handling php file that receives the $POST[]'s from a different html page with a form on it and it updates the database just fine.
<html>
<body>
<?php
$con = new mysqli("localhost", "me", "mypassword","dbName");
// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s
" , mysqli_connect_error());
exit();
}
// insert values
$qry = "INSERT INTO dbName (ID, Name, Price, Description)
VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";
// print out
if ($con->query($qry) === TRUE) {
echo "It Worked"
} else {
echo "Error: " . $qry . "<br>" . $con->error;
}
// close connection
$con->close();
?>
</body>
I am sure my problem comes from the fact that I am having to update all on one page but I am way too new at all of this to know what to do about it.