MySQL UPDATE function is not working

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
Suzanne
Forum Newbie
Posts: 10
Joined: Thu Nov 15, 2007 4:18 pm

MySQL UPDATE function is not working

Post by Suzanne »

Hi,

I need some help with this.
When I use the UPDATE function for the targeted key id nothing changes in my MySQL database. I printed all the variables and they all contain the correct values. I did not get any errors and the mysql_query for the UPDATE function returned true.

As you can see in the code below I am passing the row number as $row= $_GET['row']; then add 1 to get the id number $id_new which I will use in the UPDATE function for WHERE id='$id_new'. This should update the correct row in the table. But when I submit the changes nothing happens. Now for the very strange part. When I add 2 instead of 1 to $row it does update the values in that row. But I need the updat to happen in the row above that one.

If you need me to post the code from the form that passes the values, I will be glad to do so, but I really think the bug lays within the snippet below. I know it is something small that I overlooked. So I hope with the help of you guys I can fix this. Any help would be greatly appreciated.

usr_update_link.php:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

//variables passed from usr_links_display.php
$topic= $_GET['topic'];
$location= $_GET['link_location'];
$links= $_GET['links'];

$row= $_GET['row'];
$id_new=($row+1);
//I tried $id_new=($row+2); and it worked for the next row???????

//declare database variables
$host="localhost"; // Host name
$username="***"; // Mysql username
$password="***"; // Mysql password
$db_name="chrisuc6_Portfolio"; // Database name
$table = links;// Table name


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query="SELECT * FROM links";

$update_done =mysql_query("UPDATE links SET topic='$topic', links='$links', links_href='$location' WHERE id='$id_new'")or die(mysql_error());
mysql_close();

if($update_done)
{
	echo $id_new;
	echo "\n<br>You will be automatically relinked to the main selection page in a few seconds.";
	echo "<meta http-equiv=\"refresh\" content=\"3;URL=usr_links_change.php\">";
}
else
{
	echo "The link has not be updated\n<br>";
}

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

For your protection, I've removed your login credentials that were in the code. I'd strongly advise doing so again in the future ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$row= $_GET['row'];
$id_new=($row+1);
//I tried $id_new=($row+2); and it worked for the next row???????
What is $_GET['row'] and why do you have to add something to that value?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Without using code, can you explain what you are trying to do programmatically?
Suzanne
Forum Newbie
Posts: 10
Joined: Thu Nov 15, 2007 4:18 pm

Update values in row

Post by Suzanne »

Hi,

Thanks for protecting my website Jcart.

Basically what I am trying to achieve is to pass the variables $topic, $location, $links from an HTML form to usr_update_link.php and use those values to update $row where $id(primary key) is $row=1.

Cheers,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How are $row and $row+1 related?
Suzanne
Forum Newbie
Posts: 10
Joined: Thu Nov 15, 2007 4:18 pm

Post by Suzanne »

Sorry there was a little typo in my last post.
I meant to write $id = $row+1 not $row=1

$id is the primary key
$row is the index that I used to loop through and echo the table in a previous page

The relation ship as mentioned above is:

$id = $row+1

In essence my table looks like this

id | topic | links | location
------------------------------------------------------------
1 topic1 link name1 url1
2 topic2 link name2 url2
3 topic3 link name3 url3
4 topic4 link name4 url4
. . . . . . . . . . . .

The loop in previous page looks like that

$num=mysql_num_rows($result);
$row = 0;

Code: Select all

<table ...>

while($row < $num){
code to display row with database values ...
$row++;
}

</table>
Suzanne
Forum Newbie
Posts: 10
Joined: Thu Nov 15, 2007 4:18 pm

Issue Resolved

Post by Suzanne »

I figured it out. :D

I passed values from the form with the $_GET[] function instead of $_POST[].
Sorry for vasting your time, guys. I could slap myself for this.

Thanks, for all the great support.

Su
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad you got it worked out. It isn't a waste of time if we help people, so glad we could help.
Post Reply