Web Hosting Talk







View Full Version : PHP update script problems


amccoy
01-16-2010, 08:37 PM
I have created the Form and it is working. If i use echo i can produce all the updated field inputs. The problem seems to be with the mysql query.

Here is my code:
<?php
echo "<p>The User with ID: <b>" .$_POST["updatedid"]."</b>Was Updated!</p>";

$ud_id=$_POST['updatedid'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];
$ud_mobile=$_POST['ud_mobile'];
$ud_fax=$_POST['ud_fax'];
$ud_email=$_POST['ud_email'];
$ud_web=$_POST['ud_web'];

include("db_info.inc.php");
mysql_connect(localhost,$user,$password);

$query= 'UPDATE contacts
SET first="$ud_first", last="$ud_last", phone="$ud_phone", mobile="$ud_mobile", fax="$ud_fax", email="$ud_email", web="$ud_web"
WHERE id=$updatedid' ;
mysql_query($query);
if ($query){
echo "Update Sucessful";}
else{
echo "Update Failed";}
mysql_close();
?>



If anyone would be able to help me out and point out my error I would much appreciate it. When I run it it produces "Update Sucessful" but the updates havnt happened in the Table.

Thanks in advance.

warddr
01-16-2010, 09:06 PM
why don't you try to command this part out:
mysql_query($query);
if ($query){
echo "Update Sucessful";}
else{
echo "Update Failed";}
mysql_close();

and replace it with print($query); this will show you what the query looks like.

And By The Way; I think (didn't test) the last part of your code doesn't work
When you do this: if ($query){
the $query is still the query you entered, this will not do what you want it to do, you have to use this:

$result = mysql_query($query);
if ($result){
echo "Update Sucessful";}
else{
echo "Update Failed";}
mysql_close();

or


if (mysql_query($query)){
echo "Update Sucessful";}
else{
echo "Update Failed";}
mysql_close();

If you fix the last part of your code, you can also print mysql_error() if the query isn't ok, than you'll know what's wrong.
If I take a look at your current code it looks like your update is executed correct, but only update failed shows up because that very part of the code is wrong.

Isn't it important for webhoster to know how to script? Go learn some php or pay someone to do it for you. I'm not going to give you this kind of help every time for free..

One more thing: this code isn't safe at all, ever heard the word mysql-injection?

amccoy
01-16-2010, 09:18 PM
And By The Way; I think (didn't test) the last part of your code doesn't work

You were correct. The Code you gave produced Update Failed which is what I would expect as it isnt working.

if i run it as print($query)

I get

UPDATE contacts SET first="$ud_first", last="$ud_last", phone="$ud_phone", mobile="$ud_mobile", fax="$ud_fax", email="$ud_email", web="$ud_web" WHERE id=$updatedid

Update Failed

So I am assuming that it is somewhere in my update query (although i could be wrong)that is the problem as everything else seems to be working so the error is in:

$query= 'UPDATE contacts
SET first="$ud_first", last="$ud_last", phone="$ud_phone", mobile="$ud_mobile", fax="$ud_fax", email="$ud_email", web="$ud_web"
WHERE id=$updatedid' ;


Thanks for your help warddr hope i can get the rest of this to work

warddr
01-16-2010, 09:24 PM
You have to learn the difference between single quotes and double quotes:
This guy explains it very good:
http://v1.jeroenmulder.com/weblog/2005/04/php_single_and_double_quotes.php

Isn't it important for a webhoster to know how to script? Go learn some php or pay someone to do it for you. I'm not going to give you this kind of help every time for free..
One more thing: this code isn't safe at all, ever heard about (my)sql-injection?

amccoy
01-16-2010, 09:36 PM
ok thanks I'll read through that.

And yes that is exactly what I trying to do, learn some basic php and mysql and i thought this would be a good place to ask for help if I get stuck so if someone in the future has the same problem they can also benifit from any help given.

And yes i have heard of that but i am only beginning to learn.

Thanks for your help it is much appreciated :)

amccoy
01-16-2010, 09:51 PM
I have got it working using help from a tutorial on w3schools.com

tim2718281
01-17-2010, 05:46 AM
I have got it working using help from a tutorial on w3schools.com

Those tutorials are very good.

I've been programming for 35 years, and I *still* make mistakes like the one you did.

And sometimes I seem to get a blind spot, failing to see the obvious. Posting the problem here will often get a response from someone fresh to the problem, who can immediately see what I can't.

(And let's face it, we all like to feel superior now and then, so you shouldn't worry about giving people an opportunity to do that!)

A couple of other comments on general programming technique, though:

1) When you are baffled, always check the error logs. (If you go and look at them now, you never know, they might contain a message which would have been helpful.)

2) When you are calling routines with unexpected results, always put in some print statements that print exactly how the routine is being called.

3) Sometimes it's helpful to call the routine with values you know will fail. In your case, you'd have got "Update Successful", which would have alerted you to the test being wrong.

amccoy
01-17-2010, 08:59 AM
Thank you very much for the help tim. I will take your advice when i have my next problem :)