Jump to content

Update not working


Tasos

Recommended Posts

I can not update the table like i want, when i want to change one row everything changes when i click update ! 

so what i want is when i click update to change only the row i want and not all the rows.

 

Thanks for any help.

<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("food",$con);


if(isset($_POST['update'])){
$UpdateQuery = "
UPDATE producten SET Product='$_POST[product]', Item='$_POST[item]', Description='$_POST[description]', Extra='$_POST[extra]', Valuta='$_POST[valuta]', Price='$_POST[price]', Nummer='$_POST[nummer]' WHERE Product='$_POST[hidden]'";               
mysql_query($UpdateQuery, $con);
};

if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM producten WHERE Product='$_POST[hidden]'";          
mysql_query($DeleteQuery, $con);
};

if(isset($_POST['add'])){
$AddQuery = "INSERT INTO producten (Product, Item, Description, Extra, Valuta, Price, Nummer) VALUES ('$_POST[uproduct]','$_POST[uitem]','$_POST[udescription]','$_POST[uextra]','$_POST[uvaluta]','$_POST[uprice]','$_POST[unummer]')";         
mysql_query($AddQuery, $con);
};



$sql = "SELECT * FROM producten";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Product</th>
<th>Item</th>
<th>Description</th>
<th>Extra</th>
<th>Valuta</th>
<th>Price</th>
<th>Nummer</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=mydata6.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=product value=" . $record['Product'] . " </td>";
echo "<td>" . "<input type=text name=item value=" . $record['Item'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
echo "<td>" . "<input type=text name=extra value=" . $record['Extra'] . " </td>";
echo "<td>" . "<input type=text name=valuta value=" . $record['Valuta'] . " </td>";
echo "<td>" . "<input type=text name=price value=" . $record['Price'] . " </td>";
echo "<td>" . "<input type=text name=nummer value=" . $record['Nummer'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['Product'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=mydata6.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uproduct></td>";
echo "<td><input type=text name=uitem></td>";
echo "<td><input type=text name=udescription></td>";
echo "<td><input type=text name=uextra></td>";
echo "<td><input type=text name=uvaluta></td>";
echo "<td><input type=text name=uprice></td>";
echo "<td><input type=text name=unummer></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</form>";
echo "</table>";
mysql_close($con);

?>
Link to comment
https://forums.phpfreaks.com/topic/278294-update-not-working/
Share on other sites

First of all - never use the $_POST values directly in the query!!! Clean the values and validate them before using.

Now for your problem. Like Barand pointed out, your update query will affect all the rows that are equal to what ever you have in the $_POST['hidden'] or if you removed that it will be every row that has product = ''. 

Figure out a uniqe key for the row that you want to update and put that in you where clause.

Link to comment
https://forums.phpfreaks.com/topic/278294-update-not-working/#findComment-1431694
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.