Tasos Posted May 22, 2013 Share Posted May 22, 2013 (edited) 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 May 22, 2013 by Tasos Quote Link to comment Share on other sites More sharing options...
Barand Posted May 22, 2013 Share Posted May 22, 2013 That update query will update all rows where the value in the product column matches the value in $_POST['hidden']. Check the values. Quote Link to comment Share on other sites More sharing options...
Tasos Posted May 22, 2013 Author Share Posted May 22, 2013 I just deleted $_POST['hidden']. and it still the same Quote Link to comment Share on other sites More sharing options...
Dathremar Posted May 22, 2013 Share Posted May 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.