Web Hosting Talk







View Full Version : Tricky database modification question


Energizer Bunny
12-07-2009, 07:10 PM
Maybe someone here can help me achieve what i am trying to achieve with database.

I have following problem :

I am using a code similar to this :


do {
while ($row = mssql_fetch_array($var1)) {
echo '<tr><td style="width:80px;border-bottom:thin solid #666666;"><input type="text" value='.$row['user_var1'].' name="var1[]"/></td><td> <input type="text" value='.$row['var2'].' name="var2[]"/>'</td></tr>';


}
} while (mssql_next_result($var1));


Now what I am trying to achieve is modification of database using
$value = $_POST['user_var1'];
$valuexyz = $_POST['user_var2'];
$query= "UPDATE user_db1 SET user_var1 = '$value' WHERE user_var2='$valuexyz' ";
mssql_query($query);


Its not the exact code just trying to provide some idea.

What i am trying to do is have an auto generated form field, with values fetched from database and editable inside the do while loop so multiple rows are generated via database, now i am trying to modify one single row on that auto generated form, but am not able to modify just 1 particular row, var2 is common to all rows or user_var2 and in query you can see that it will update every row where it find user_var2 and not just 1 particular row that i am trying to update.

Any ideas on how to update just 1 single row even though the where clause is such that it consists of various similar entires.

Say for example i am trying to edit someones named "smith" phone number, but there are multiple smith's , and updating one , results in updating every smith's phone numbers.

Smith example http://www.sql-tutorial.net/SQL-UPDATE.asp

look at the bottom, but i want to modify only say one on the top and not the bottom entry.

Thanks i hope there is a solution to this weirdness.

foobic
12-07-2009, 08:14 PM
Any ideas on how to update just 1 single row even though the where clause is such that it consists of various similar entires.You don't. Instead, use a hidden field in the form containing a unique identifier, typically the primary key, and put that in your where clause.

Keep in mind that even if it's "hidden", the incoming data cannot be trusted - so make sure you filter it to accept only what you're expecting and ensure that the user is allowed to make the modification requested.

Energizer Bunny
12-08-2009, 01:06 PM
I suppose there was no other way of doing what I wanted hence did what foobic told me too. Users will be only few say less than 10 and they are no hackers i think so :) .