I've been trying all kinds of tutorials and solutions on here and I can not get this to work. Please help!
** UPDATE #1 **
This is the error that I receive when running my code: "Error occured in the insert: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
** UPDATE #2 **
Here is the page that shows everything in the table: editBrowse.php
`
$sql = "SELECT * FROM inventory";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "
<img src=$row[image] id='productImage'> <br>
product: $row[product] <br>
category: $row[category] <br>
seller: $row[seller] <br>
<a href='edit.php?id=$row[id]'>Edit</a>
<hr>
";
}
} else {
echo "No Results";
}
$connection->close();
`
This is the page that shows up when you click the edit button above: edit.php
`
<?php
// Connection code..etc..
$sql = "SELECT * FROM inventory WHERE id=".$_REQUEST['id'];
$result = $connection->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
} else {
echo "No Results";
}
$connection->close();
?>
<div class="row" id="mainSection">
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?=$id?>">
<h3>Image:</h3>
<input type="text" name="image" value="<?=$row['image']?>">
<h3>Product:</h3>
<input type="text" name="product" value="<?=$row['product']?>">
<h3>Category:</h3>
<input type="text" name="category" value="<?=$row['category']?>">
<h3>Seller:</h3>
<input type="text" name="seller" value="<?=$row['seller']?>">
<br>
<br>
<input type="submit" value="Update My Record">
</form>
<br>
<a href="admin.php" id="backButton">Back</a>
</div>
`
And this is the file that runs when you click the "Update My Record" button above: update.php
'
$id= $_POST['id'];
$image = $_POST['image'];
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];
$sql = "UPDATE inventory set image='$image', product='$product', category='$category', seller='$seller' WHERE id = $id";
if ($connection->query($sql) === true) {
echo "Inserted Successfully";
} else {
echo "Error occured in the insert: " . $connection->error;
}
$connection->close();
'
You have a syntactic problem in your sql:
$sql = "UPDATE inventory set image='$image', product='$product', category='$category', seller='$seller' WHERE id = $id";
In addition you need to verify if $id has the correct value. Because if it's empty or null your query will fail.
If you want to insert data you can use a query like this:
$sql = "INSERT INTO inventory (image, product, category, seller) VALUES ('$image', '$product', '$category', '$seller')";
Try this in your edit.php view:
<?php
$sql = "SELECT * FROM inventory WHERE id=".$_REQUEST['id];
$result = $connection->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
} else {
echo "No Results";
}
$connection->close();
?>
<div class="row" id="mainSection">
<form action="update.php" method="post">
<input type="hidden" name="id" value="value="<?php echo $row['id']?>"">
<h3>Image:</h3>
<input type="text" name="image" value="<?php echo $row['image']?>">
<h3>Product:</h3>
<input type="text" name="product" value="<?php echo $row['product']?>">
<h3>Category:</h3>
<input type="text" name="category" value="<?php echo $row['category']?>">
<h3>Seller:</h3>
<input type="text" name="seller" value="<?php echo $row['seller']?>">
<br><br>
<input type="submit" value="Update My Record">
</form>
<br>
<a href="admin.php" id="backButton">Back</a>
</div>
You need to set a value for the id
, and then use $_POST['id']
in your query.
First of all the sql syntax for update query is wrong. Secondly there is no value set in the id in the form. So when you post the form the value will be empty.
$sql = "SELECT * FROM inventory";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "
<img src=$row['image'] id='productImage'> <br>
product: $row['product'];<br>
category: $row['category']; <br>
seller: $row['seller'] ;<br>
<a href="edit.php?id=$row['id']">Edit</a>
<hr>
";
}
} else {
echo "No Results";
}
$connection->close();
EDIT.PHP
<div class="row" id="mainSection">
<?php $id = $_REQUEST['id']; ?>
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?=$id?>">
<h3>Image:</h3>
<input type="text" name="image">
<h3>Product:</h3>
<input type="text" name="product">
<h3>Category:</h3>
<input type="text" name="category">
<h3>Seller:</h3>
<input type="text" name="seller">
<br>
<br>
<input type="submit" value="Update My Record">
</form>
<br>
<a href="admin.php" id="backButton">Back</a>
</div>
UPDATE.PHP
<?php
$id= $_POST['id'];
$image = $_POST['image'];
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];
$sql = "UPDATE inventory set image='".$image."', product='".$product."', category='".$category."', seller='".$seller."' WHERE id = ".$id." ";
if ($connection->query($sql) === true) {
echo "Inserted Successfully";
} else {
echo "Error occured in the insert: " . $connection->error;
}
$connection->close();