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);

?>
Edited by Tasos
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.