Forum Moderators: coopster

Message Too Old, No Replies

Form not updating database

Form returns no errors but it doesn't update the table. Beginner- be gentle

         

DScoffers

12:54 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



I'm only a beginner and have only started learning PHP and MySQL today.
I've used some sample scripts from a tutorial site, but can't get them to work.

Output.php shows a pre-filled form based on the id (primary index) from the url www.site.com/output.php?id=6. The idea is to amend the details, click Update and it saves it to the table.

It says that records have been updated on the results page updated.php, (but that's just a simple echo).
I've put in what I think are error checks, but it gives no errors.
I'm sure it's something simple (like parts in the wrong place), but hopefully you guys can help.
===================
OUTPUT.PHP
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id;?>">
First Name: <input type="text" name="ud_first" value="<? echo $first;?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo $last;?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo $phone;?>"><br>
<input type="Submit" value="Update">
</form>

<?
$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];
?>

<?
++$i;
}
mysql_close();
?>
===================
UPDATED.PHP
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query=" SELECT * FROM contacts WHERE id='$id'";

$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];

mysql_query($query) or die("contacts update failed<br>$query" . mysql_error());
$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'";

mysql_close();
echo "Record Updated";
?>
===================

dreamcatcher

1:57 pm on Aug 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello DScoffers,

Welcome to Webmaster World. :)

It sounds like your $id variable isn`t populating, which would be down to register globals being OFF. Try accessing you variable using $_GET['id']

dc

smatts9

3:36 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



I don't see anywhere where you set $id = to anything?

DScoffers

4:05 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



Cheers dreamcatcher,
I get the feeling I'll be here quite abit from now on. ;-}

$_GET['id'] - Which file would this be put in and where?
How do you switch register globals to ON?
(I use phpMyAdmin by the way)
I've just thought - maybe I needed an extra line:
$id=mysql_result($result,$i,"id");
in output.php?

smatts9 - The output.php file populates fully with the correct details for the id from the url "output.php?id=6", and it works properly for whatever id I put at the end of the url.

$query=" SELECT * FROM contacts WHERE id='$id'";

Is this enough to set the id?

dreamcatcher

5:46 pm on Aug 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DScoffers,

Before you use the $id variable, set it like this:

$id = $_GET['id'];

Register Globals can be activated or de-activated in your PHP.ini file. Its generally known to be more secure and better coding practice if you leave them off.

dc

smatts9

6:58 pm on Aug 10, 2006 (gmt 0)

10+ Year Member




Before you use the $id variable, set it like this:

$id = $_GET['id'];

That's what I was talking about.

DScoffers

1:35 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



OK, I've changed both files to the following with no luck.
I've tried it with $id = $_GET['id']; and have changed the latest version to
============
OUTPUT.PHP
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id=$_GET['id'];
$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id;?>">
First Name: <input type="text" name="ud_first" value="<? echo $first;?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo $last;?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo $phone;?>"><br>
<input type="Submit" value="Update">
</form>
<?
$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];
?>
<?
++$i;
}
mysql_close();
?>

===============

UPDATED.PHP

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$ud_id=$_GET['ud_id'];
$query=" SELECT * FROM contacts WHERE id='$ud_id'";

$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];

mysql_query($query) or die("contacts update failed<br>$query" . mysql_error());
$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'";

mysql_close();
echo "Record Updated";
echo $ud_id;
echo $ud_first;
echo $ud_last;
echo $ud_phone;
?>
===========
I've put in echo: the ud_*** results and they appear fine on updated.php, but don't save to the database.

Still no luck:
Have I put it $id=$_GET['id']; in the wrong place?
I changed it in updated.php to $ud_id=$_GET['ud_id']; as ud_id is the output from output.php. Is that right? (I tried with just id as well)

Do I need:
$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];

in both files?

dreamcatcher

1:56 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your updated.php file, change to:

$ud_id=$_POST['ud_id'];

Because the variable is then coming from the form and not the url. Also, its always a good idea to set your error reporting level to E_ALL. At the top of your PHP scripts place the following:

error_reporting(E_ALL);

This can help root out problems in the future.

dc

DScoffers

2:58 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



OK, I've changed it to $ud_id=$_POST['ud_id']; in updated.php
(I see where I went wrong there now, ta)
and added the E_ALL for debugging.

It now comes back with errors on output.php:
Undefined index: ud_id in html/output.php on line 42(each line a ud_*** is used)
and
Use of undefined constant localhost - assumed 'localhost' in html/output5.php on line 6
(Should I have
$localhost="www.mydomain.com";
mysql_connect($localhost,$username,$password);
?
Or is presuming localhost ok, as that is what I am using?

I've had a look in mysql, and the id field is set as
PRIMARY UNIQUE and INDEX, which gives an error. Oops.
The id is an INT set to auto-increment, I've changed it to just INDEX
but it still doesn't work.

From using ASP pages the index would be unique and the primary index as well, what should the settings be here?
(I'm getting closer - I can feel it. ;-})

dreamcatcher

3:17 pm on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, going through those things one at a time:

The $ud_id undefined error is because you are accessing the variable before its been set. Use the isset [uk2.php.net] function to clear that up:

$ud_id = isset($_POST['ud_id'])? $_POST['ud_id'] : '';

For your localhost problem, you need to include localhost in apostrophes or quotes. If you don't the system will think its a constant, which are defined like this:

define('VAR_NAME', 1);

and accessed as such:

echo VAR_NAME;

So, change this:

mysql_connect(localhost,$username,$password);

to this:

mysql_connect('localhost',$username,$password);

Your mysql field needs to just simply be something like:

id INT (5) NOT NULL auto_increment

dc

Romeo

3:18 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



I've put in echo: the ud_*** results and they appear fine on updated.php, but don't save to the database.

It doesn't save, because you don't save.

In your updatead.php you pre-define a select query string in line 4, but is it needed at all?
$query=" SELECT * FROM contacts WHERE id='$ud_id'";

In line 11 you execute that "select *":
mysql_query($query)...;
But there is no reason to do that, and nothing is done with the result.

In line 12 you then pre-define another query string, but you never execute it.
$query="UPDATE contacts SET ..."

So, put line 12 in front of line 11, and delete line 4.

And during debugging, don't forget to output an
echo $query;
to check if everything looks correct at all.

And you may get rid of using the $_GET['..'] in line 3 as well, as you call the updated.php with the "method=POST" from your form and read these POSTed variables later from $_POST...

Kind regards,
R.

DScoffers

3:54 pm on Aug 11, 2006 (gmt 0)

10+ Year Member



BINGO!

So I only need $query=" SELECT * FROM contacts WHERE id='$ud_id'"; in OUTPUT.PHP then. ok.

And I was baically saying
Line 11: Run the query
Line 12: query is to set the field values

But they needed to be the other way round - (obvious when you put it like that Romeo)

Thanks to everyone for solving this, their little hints and tips and watch out here for the next installment of "beginner goes 'huh?'" ;-}