 |
 |
A little PHP/MySQL help
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Hello, I'm having trouble with a PHP script. It is supposed update my MySQL db but I can't get it to work right. All I get is a blank page. In my DB id is set as unique and auto increment. Any ideas? Thanks!
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id=$_GET['id'];
$query="SELECT * FROM lay WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>
<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>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo $mobile; ?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo $fax; ?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo $web; ?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: Denver, CO, USA
Status:
Offline
|
|
maybe try this:
Code:
for ($i=0;$i<$num;$i++)
{ $first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>
<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<?=$id?>">
First Name: <input type="text" name="ud_first" value="<?=$first?>"><br>
Last Name: <input type="text" name="ud_last" value="<?=$last?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<?=$phone?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<?=$mobile?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<?=$fax?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<?=$email?>"><br>
Web Address: <input type="text" name="ud_web" value="<?=$web?>"><br>
<input type="Submit" value="Update">
</form>
<?
}
?>
(Last edited by hotani; Apr 7, 2005 at 11:08 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Mpls, MN
Status:
Offline
|
|
I didn't think mysql_numrows() was a function, its not on php.net. I always use mysql_num_rows(). But I tested them and they both work.
If by "plank page" you mean that your are not even getting any output such as the form tags then you most likely have an error and you have your php.ini file configured to not output the errors. This can be changed during runtime.
Here's an easier way to accomplish your same goal:
Code:
<?php
$query="SELECT * FROM lay WHERE id='$id'";
$result=mysql_query($query) or die(mysql_error().'<br/>'.$query); // output mysql error
// loop through each row and set $row
// to an associative array of your fields
// concatenate everything into one big string
while($row = mysql_fetch_assoc($result)) {
// now you can access your fields like this:
// $row['email'] or $row['fax']
// the .= will just add on to whatever $output is already set to
$output .=
'<form>
<input type="text" name="email" value="'.$row['email'].'" />
</form>';
}
echo $output;
?>
Doing just one echo is faster and usually results in cleaner code. Hope this helps and doesn't just confuse you.
It is really helpful to add the "or die(myql_error().'<br />'.$query)" to every query because it will let you know if your query is wrong.
|

Thus, creating the most ginormous can of whoop ass the world as ever seen.
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
I still get a blank age for both of those above examples. It seems to all stem from the WHERE id='$id' part because when I remove that the page loads. I can't use the script with that WHERE id='$id' because the script needs that identify what entry to update.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Mpls, MN
Status:
Offline
|
|
Are you sure you have error reporting turned on?
Try adding this to the top of your script:
error_reporting(E_ALL);
// and or
ini_set("display_errors", "1");
I'm also confused about your query. Your selecting based on id='$id'. Is id a unique key for the table? If so, this query will only return 1 row.
I'd try just doing "SELECT * FROM lay" and see if you get anything.
|

Thus, creating the most ginormous can of whoop ass the world as ever seen.
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Originally posted by MacAtak:
Are you sure you have error reporting turned on?
Try adding this to the top of your script:
error_reporting(E_ALL);
// and or
ini_set("display_errors", "1");
I'm also confused about your query. Your selecting based on id='$id'. Is id a unique key for the table? If so, this query will only return 1 row.
I'd try just doing "SELECT * FROM lay" and see if you get anything.
Yeah, I have display_errors on in my php.ini.
Yep, id is unique
I am using WHERE id='$id' because I thought I needed to use id to allow the script to identity which record to update.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Mpls, MN
Status:
Offline
|
|
The only time the query will need to know what record to update is when you are doing an update such as:
Code:
UPDATE lay SET email="foo" WHERE id="id"
Your form is submitting the id to your update.php file. In this file is where you need to know which record is being updated. You'd want something like this in there:
Code:
UPDATE lay SET email="$_POST['ud_email']" WHERE id="$_POST['ud_id']"
If you are trying to display all the rows from the table lay you do not want to have the WHERE clause.
|

Thus, creating the most ginormous can of whoop ass the world as ever seen.
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
This is the tutorial I was following. This is exactly what I want to do.
click here
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Mpls, MN
Status:
Offline
|
|
Ah, ok, yeah I was pretty confused. I thought you wanted to display a form for every record in the database, which wouldn't really make sense.
I'm pretty sure the "localhost" part of your mysql_connect() needs to be a string:
Code:
mysql_connect('localhost', $username, $password);
Other than that and the odd way the tutorial is getting the field values I can't see anything wrong with your code. You could try getting rid of the @ symble in front of mysql_select_db, I've never used that.
p.s. i need to read more carefully and ease up on the posting trigger
|

Thus, creating the most ginormous can of whoop ass the world as ever seen.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2003
Location: London
Status:
Offline
|
|
I can't get the original tutorial to load so I can't look at that.
If the code works without the extra MySQL WHERE statement, then that is somewhere to start looking - you're getting the id via the GET array, you are supplying the id to the GET array in the URL? (not trying to be funny, sometimes its the simple things).
I would suggest you look into arrays, you could have an array that uses the names of the MySQL values as keys, you could then get it to process each value and also pass to the POST array using the same key. You could go further and use the keys to create a linked array or multi-dimensional array, and hold what type of input HTML is making and get the whole process automated and defined by one array.
Takes a bit more effort in the short term, but long term

|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Originally posted by moodymonster:
...you're getting the id via the GET array, you are supplying the id to the GET array in the URL?
I think that this is the issue but I don't know how to fix it.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2003
Location: London
Status:
Offline
|
|
I noticed you've got quote marks around the 'id' in the code
$id = $_GET['id'];
I've not used that, I wouldn't have thought that'd be a problem though, I usually use:
$id = $_GET[id];
then supply to the code via the address bar in the browser:
my_file_name.php?id=the_id_value_here
if you want multiple values supplied use the format:
my_file_name.php?id=the_id_value_here&other_va lue=other_value_content
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2003
Location: London
Status:
Offline
|
|
you could also change:
$result=mysql_query($query);
to:
$result = mysql_query($query) or die(mysql_error());
it will then give you the MySQL errors.
you could also echo the $id value straight after its been retrieved from the GET array, that way you can see what is being retrieved. You could leave this bit in, even if its not being used by the MySQL query.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
God this is so frustrating!!
I went to my address bar entered mysite/update.php?id=1
It pulled the entry but when I hit update it says the the record has been updated but nothing is changed in my DB.
<?
include("dbinfo.inc.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);
mysql_close();
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>
<form action="updated.php">
<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>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>
Second PHP
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
$ud_id=$_POST[ud_id];
$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];
$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='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Originally posted by moodymonster:
you could also change:
$result=mysql_query($query);
to:
$result = mysql_query($query) or die(mysql_error());
it will then give you the MySQL errors.
you could also echo the $id value straight after its been retrieved from the GET array, that way you can see what is being retrieved. You could leave this bit in, even if its not being used by the MySQL query.
No errors were shown.
I added a <? echo "$id"; ?> to my page that displays all my data the id cells were empty.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2003
Location: London
Status:
Offline
|
|
<form action="updated.php">
needs to tell the browser what to do
<form action="updated.php" method=POST>
also if you change (this bit isn't strictly necessary):
mysql_query($query);
echo "Record Updated";
to read:
$update_query = mysql_query($query);
if($update_query){
echo "Record Updated";
} else {
echo "Failed to Update";
}
otherwise the code will tell you its updated regardless
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Originally posted by moodymonster:
<form action="updated.php">
needs to tell the browser what to do
<form action="updated.php" method=POST>
also if you change (this bit isn't strictly necessary):
mysql_query($query);
echo "Record Updated";
to read:
$update_query = mysql_query($query);
if($update_query){
echo "Record Updated";
} else {
echo "Failed to Update";
}
otherwise the code will tell you its updated regardless
OK, I added the above info and now it says Failed to Update.
PS: Would you like to take this to IM?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2003
Location: London
Status:
Offline
|
|
stick this line into the second PHP file after the password bit:
mysql_select_db($database) or die( "Unable to select database");
I didn't see that missing last time
I removed the @ sign, this tells the script to supress errors
does it work now?
I'm on iChat/AOL as donjamie1
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status:
Offline
|
|
For completeness, please let us know what the final solution was...
|
Scott Genevish
scott AT genevish DOT org
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2005
Location: Central IL
Status:
Offline
|
|
I would recommend implementing a third-party mysql database abstraction layer, so every database task you need to perform isn't bogged down by mechanical connection issues. There are plenty of them out there, and they're generally quite simple to implement.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Aug 2002
Status:
Offline
|
|
Originally posted by genevish:
For completeness, please let us know what the final solution was...
Final Solution was...
First PHP file:
<?
include('Include.inc');
$conn = mysql_connect($DBhost,$DBuser,$DBpass);
mysql_select_db("$DBName", $conn) or die("Unable to select database");
$id=$_GET[id];
$query="SELECT * FROM $table Where id='$id'";
$result = mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$email=mysql_result($result,$i,"email");
?>
<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>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>
Second PHP file:
<?
include('Include.inc');
$conn = mysql_connect($DBhost,$DBuser,$DBpass);
mysql_select_db("$DBName", $conn) or die("Unable to select database");
$ud_id=$_POST[ud_id];
$ud_first=$_POST[ud_first];
$ud_last=$_POST[ud_last];
$ud_phone=$_POST[ud_phone];
$ud_email=$_POST[ud_email];
$query="UPDATE $table SET first='$ud_first', last='$ud_last', phone='$ud_phone', email='$ud_email' WHERE id='$ud_id'";
$update_query = mysql_query($query);
if($update_query){
echo "Record Updated";
} else {
echo "Failed to Update";
}
mysql_close();
?>
And once again, I thank moodmonster for helping me with this!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|