Updating ID number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Updating ID number

Post by daebat »

I need to call to the database, display by id number (editable), and then submit for changes. What am I doing wrong here?

Code: Select all

<?
mysql_connect("localhost", "user", "pdub") or die(mysql_error()) ;
mysql_select_db("table") or die(mysql_error()) ;
 
if(!isset($cmd)) 
{
 
   $result = mysql_query("select * from test order by id"); 
   
   while($r=mysql_fetch_array($result)) 
   { 
 
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
      $thumb=$r["thumb"];
      
      echo "<form><table>
        <tr>
        <td><input type='text' name='id' value='$id' size='3'></td><td width='350px'><strong>$title</strong></td>
        </tr>
        <td colspan='2' align='center'><img src='upload/test/$thumb'></td>
        </table></form>
         ";
    }
}
?>
<?
echo "<div style='text-align:center;'><a href='testorder.php?cmd=order&id=$id'>Submit</a></div>";
?>
<?
if($cmd=="order")
{
    $sql = "UPDATE test SET id=$id";
    $result = mysql_query($sql);
    echo "UPDATED!";
    echo "<a href='main.php'>Click here to return to the Administration Area.</a>";
}
}
 
 
?>
 
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Updating ID number

Post by lshaw »

I have a feeling it could be this bit.

Code: Select all

$sql = "UPDATE test SET id=$id";
    $result = mysql_query($sql);
    echo "UPDATED!";
    echo "<a href='main.php'>Click here to return to the Administration Area.</a>";
You need to put $id in quotation marks.

Code: Select all

$sql = "UPDATE test SET id='$id'";
I am not sure, but I think you must also have a where

Code: Select all

$sql = "UPDATE test SET id='$id' WHERE x='y'";
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Updating ID number

Post by requinix »

Quotation marks aren't necessary - in fact, if the id is a number then you shouldn't use quotes - but without that WHERE it'll update every row in the table.
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Updating ID number

Post by daebat »

$sql = "UPDATE test SET id='$id' WHERE x='y'";
What would x and y be? You were right about the quotes around $id so thank you for that. Still not working though.
nothing1306
Forum Newbie
Posts: 4
Joined: Tue Jan 26, 2010 6:18 am

Re: Updating ID number

Post by nothing1306 »

:banghead:

This file name is testorder.php:
<? // <?php
mysql_connect("localhost", "user", "pdub") or die(mysql_error()) ;
mysql_select_db("table") or die(mysql_error()) ;
// $cmd = $_GET["cmd"];
// $id = $_GET["id"];
if(!isset($cmd))
{

$result = mysql_query("select * from test order by id");

while($r=mysql_fetch_array($result))
{

$title=$r["title"];//take out the title
$id=$r["id"];//take out the id

$thumb=$r["thumb"];

echo "<form><table>
<tr>
<td><input type='text' name='id' value='$id' size='3'></td><td width='350px'><strong>$title</strong></td>
</tr>
<td colspan='2' align='center'><img src='upload/test/$thumb'></td>
</table></form>
";
}
}
?>
<? // <?php
echo "<div style='text-align:center;'><a href='testorder.php?cmd=order&id=$id'>Submit</a></div>";
/*
* echo "<div style='text-align:center;'><a href='testorder.php?cmd=order&id=$id'>Submit</a></div>";
*/

?>
<? // <?php
if($cmd=="order")
{
// Is "id" primari key ?
$sql = "UPDATE test SET id=$id";
$result = mysql_query($sql);
echo "UPDATED!";
echo "<a href='main.php'>Click here to return to the Administration Area.</a>";
}
} // <~~~ error


?>
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Updating ID number

Post by lshaw »

It could be WHERE id=$id (the current id) but i can't tell if you are storing the current id in a variable
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Updating ID number

Post by daebat »

This file name is testorder.php:
Nothing... you bolding the code doesn't mean anything to me. I am pretty new to PHP but am starting to understand some of this stuff. Why would I use the $GET statement for CMD? ID is the primary key.
nothing1306
Forum Newbie
Posts: 4
Joined: Tue Jan 26, 2010 6:18 am

Re: Updating ID number

Post by nothing1306 »

Why would I use the $GET statement for CMD?
Why? :crazy: I don't remember... I did forget it

Sometimes... I don't know what I did or said... Can anybody here help me?
Post Reply