Forum Moderators: coopster

Message Too Old, No Replies

Well, my script successfully changes the db entry

But it also changes EVERY entry.. Not sure why

         

BadGoat

1:04 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Hi!

This sceipt below is supposed to take a db entry by id and allow me to alter just the $diwtitle variable. This it does successfully.. But after I change it and go to MySQL and look at the db, it has changed the $diwtitle variable in every row of the table. And that was not an intended outcome.

How can I make it change just the one entry?

[php]
<?php
// Connect to database
$connect= mysql_connect("localhost","root")
or die("Could not connect to database in localhost!");
mysql_select_db("testdiw")
or die("Could not select that database!");


if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'update')
{
$id = $_REQUEST['id'];
$_GET['id'] = $id;
$query = "UPDATE `mktime` SET diwtitle = '".$_REQUEST['newtitle']."'";
$result = mysql_query($query) or die("<b>mySQL Error:</b>".mysql_errono()."<br>".mysql_error());
if(!$result)
{
echo 'Error processing request.';
}
else
{
echo 'Request processed successfully.';
}
}

$id = $_GET['id'];
// The ID is passed through the URL to specify the row,
// Or it is set in the previous script.

$query = "SELECT * FROM mktime WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo '<form name="update" method="post">
<input type="hidden" value="update" name="action">
<input type="hidden" name="id" value="'.$id.'">
<input type="text" name="newtitle" value="'.$row['diwtitle'].'"><br>
<input type="submit" value="Update">
</form>';
?> [/php]

topr8

1:09 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$query = "UPDATE `mktime` SET diwtitle = '".$_REQUEST['newtitle']."' WHERE COLUMNNAME=CRITERIA";

BadGoat

1:30 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



WHERE COLUMNNAME=CRITERIA literally? I tried that and tried replacing CRITERIA with diwtitle, 'diwtitle', and everything else I can think of..

topr8

1:45 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



not literally! probably something like this ...

WHERE id = $id

[you need to tell it which rows to update, otherwise all rows will be updated]

BadGoat

1:50 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



$query = "UPDATE `mktime` SET diwtitle = '".$_REQUEST['newtitle']."' WHERE id='$id'";

that worked.. only changed the one line.. :)

EDIT: Thank you topr! Looks like I figured out how to do exactly what you intended me to do.. For all I know, CRITERIA may be some MySQL function which I had never heard aobut.. lol