Jump to content


Photo

updating mysql with php


  • Please log in to reply
8 replies to this topic

#1 elginwick

elginwick
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 May 2006 - 03:18 PM

hello,

i have been trying to write an update code, but it isn't working.
any suggestions.

i link from one php code that allows me to pick which information i want to see based on an id, it then refers
to this page

<?php
$conn = mysql_connect("localhost", "username", "somepass")
or die(mysql_error());
mysql_select_db("selectdatakbase",$conn) or die(mysql_error());

$ud_id=$_POST['ud_id'];
$f_name=$_POST['f_name'];
$ud_l_name=$_POST['ud_l_name'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_zipcode=$_POST['ud_zipcode'];
$ud_phone=$_POST['ud_phone'];
$ud_phone2=$_POST['ud_phone2'];
$ud_session=$_POST['ud_session'];
$ud_gdate=$_POST['ud_gdate'];
$ud_status=$_POST['ud_status'];
$ud_contact=$_POST['ud_contact'];
$ud_refer=$_POST['ud_refer'];
$ud_referphone=$_POST['ud_referphone'];



$query = "UPDATE data while f_name = '$ud_f_name', $l_name = '$ud_l_name', $city = '$ud_city', $state = '$ud_state', zipcode = '$ud_zipcode', phone = '$ud_phone', phone2 = '$ud_phone2', session = '$ud_session', gdate = '$ud_gdate',status = '$ud_status', contact = '$ud_contact', refer = '$ud_refer',
referphone = '$ud_referphone' WHERE id = '$ud_id'";
mysql_query($query) or die(mysql_error());
echo "Record Updated";
mysql_close();
?>

i don't know if it makes any sense, but any help would be greatly appreciated.

thanks.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 16 May 2006 - 03:58 PM

1) this:
$ud_id=$_POST['ud_id'];
$f_name=$_POST['f_name'];
$ud_l_name=$_POST['ud_l_name'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_zipcode=$_POST['ud_zipcode'];
$ud_phone=$_POST['ud_phone'];
$ud_phone2=$_POST['ud_phone2'];
$ud_session=$_POST['ud_session'];
$ud_gdate=$_POST['ud_gdate'];
$ud_status=$_POST['ud_status'];
$ud_contact=$_POST['ud_contact'];
$ud_refer=$_POST['ud_refer'];
$ud_referphone=$_POST['ud_referphone'];

can be turned into this:
extract($_POST);

That will do the same thing.

2) As far as the update, are you getting any errors? And might I suggest that you not use the word "session" as a column name as that is a PHP reserved word?

Info: PHP Manual


#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 05:53 PM

besides the column name "session", it's UPDATE SET, not UPDATE WHILE
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 elginwick

elginwick
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 May 2006 - 05:57 PM

I have reworked it a bit, and now this is what i have

<?php

$ud_id=$_POST['ud_id'];
$ud_f_name=$_POST['ud_f_name'];
$ud_l_name=$_POST['ud_l_name'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_zipcode=$_POST['ud_zipcode'];
$ud_phone=$_POST['ud_phone'];
$ud_phone2=$_POST['ud_phone2'];
$ud_session=$_POST['ud_session'];
$ud_gdate=$_POST['ud_gdate'];
$ud_status=$_POST['ud_status'];
$ud_contact=$_POST['ud_contact'];
$ud_refer=$_POST['ud_refer'];
$ud_referphone=$_POST['ud_referphone'];

$conn = mysql_connect("localhost", "username", "somepass")
or die(mysql_error());
mysql_select_db("somedb",$conn) or die(mysql_error());

mysql_query ("Update data SET f_name = '$ud_f_name', l_name = '$ud_l_name', city = '$ud_city', state = '$ud_state',
zipcode = '$ud_zipcode', phone = '$ud_phone', phone2 = '$ud_phone2', session = '$ud_session', gdate = '$ud_gdate',
status = '$ud_status', contact = '$ud_contact', refer = '$ud_refer', referphone = '$ud_referphone' WHERE id = '$ud_id'") or die(mysql_error());
echo "Record Updated";
mysql_close();


?>

the problem is that it seems to work, as in it says record updated, but it doesn't actually updat anything.
i have tried everything, and i don't understand what to do next.
thanks in advance.

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 06:03 PM

what you should do now is echo the query string.
$sql = "Update data SET f_name = '$ud_f_name', l_name = '$ud_l_name', city = '$ud_city', state = '$ud_state',
zipcode = '$ud_zipcode', phone = '$ud_phone', phone2 = '$ud_phone2', session = '$ud_session', gdate = '$ud_gdate',
status = '$ud_status', contact = '$ud_contact', refer = '$ud_refer', referphone = '$ud_referphone' WHERE id = '$ud_id'";
mysql_query($sql) or die(mysql_error());
echo "$sql <br>";
echo "Record Updated";
mysql_close();

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 elginwick

elginwick
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 May 2006 - 06:27 PM

i changed the session name to a different name and added the echo $sql.

it told me what i did but didn't add anything into the database.

maybe it would help if i posted some of the code from the page where you can choose
what record you want to look at.


[code=auto:0]
if ($_POST[sel_id] == "") {
header("Location: somepage.php");
exit;

}

$query=" SELECT * FROM db WHERE id='$_POST[sel_id]'";
$result=mysql_query($query);
$num=mysql_numrows($result);


$i=0;
while ($i < $num) {
$f_name=mysql_result($result,$i,"f_name");
$l_name=mysql_result($result,$i,"l_name");
$street=mysql_result($result,$i,"street");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zipcode=mysql_result($result,$i,"zipcode");
$phone=mysql_result($result,$i,"phone");
$phone2=mysql_result($result,$i,"phone2");
$classnum=mysql_result($result,$i,"classnum");
$gdate=mysql_result($result,$i,"gdate");
$status=mysql_result($result,$i,"status");
$contact=mysql_result($result,$i,"contact");
$refer=mysql_result($result,$i,"refer");
$referphone=mysql_result($result,$i,"referphone");

$display_block = "
<form method=\"post\" action=\"updated.php\">
<input type=\"hidden\" name=\"ud_id\" value=\" $id \">
First Name: <input type=\"text\" name=\"ud_f_name\" value=\" $f_name \"><br>
Last Name: <input type=\"text\" name=\"ud_l_name\" value=\" $l_name \"><br>
Street: <input type=\"text\" name=\"ud_street\" value=\" $street \"><br>
City: <input type=\"text\" name=\"ud_city\" value=\" $city \"><br>
State: <input type=\"text\" name=\"ud_state\" value=\" $state \"><br>
Zipcode: <input type=\"text\" name=\"ud_zipcode\" value=\" $zipcode \"><br>
Phone: <input type=\"text\" name=\"ud_phone\" value=\" $phone \"><br>
Phone2: <input type=\"text\" name=\"ud_phone2\" value=\" $phone2\"><br>
Session: <input type=\"text\" name=\"ud_classnum\" value=\" $classnum \"><br>
Grad Date: <input type=\"text\" name=\"ud_gdate\" value=\" $gdate \"><br>
Current Status: <input type=\"text\" name=\"ud_status\" value=\" $status\"><br>
Last Contact: <input type=\"text\" name=\"ud_contact\" value=\" $contact\"><br>
Referral Person: <input type=\"text\" name=\"ud_refer\" value=\" $refer\"><br>
Referral Person Phone: <input type=\"text\" name=\"ud_referphone\" value=\" $referphone \"><br>
<input type=\"Submit\" value=\"add\">
</form>";
++$i;
}
}


?>
<html>
<head>
<title>
</title>
</head>
<body>
<?php echo $display_block; ?>
</body>

[code=auto:0]


then the second page that the form posts to is

[code=auto:0]
<?php

$ud_id=$_POST['ud_id'];
$ud_f_name=$_POST['ud_f_name'];
$ud_l_name=$_POST['ud_l_name'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_zipcode=$_POST['ud_zipcode'];
$ud_phone=$_POST['ud_phone'];
$ud_phone2=$_POST['ud_phone2'];
$ud_classnum=$_POST['ud_classnum'];
$ud_gdate=$_POST['ud_gdate'];
$ud_status=$_POST['ud_status'];
$ud_contact=$_POST['ud_contact'];
$ud_refer=$_POST['ud_refer'];
$ud_referphone=$_POST['ud_referphone'];

$conn = mysql_connect("localhost", "username", "somepass")
or die(mysql_error());
mysql_select_db("somedb",$conn) or die(mysql_error());

$sql = "Update data SET f_name = '$ud_f_name', l_name = '$ud_l_name', city = '$ud_city', state = '$ud_state',
zipcode = '$ud_zipcode', phone = '$ud_phone', phone2 = '$ud_phone2', classnum = '$ud_classnum', gdate = '$ud_gdate',
status = '$ud_status', contact = '$ud_contact', refer = '$ud_refer', referphone = '$ud_referphone' WHERE id = '$ud_id'";
mysql_query($sql) or die(mysql_error());
echo "$sql <br>";
echo "Record Updated";
mysql_close();


?>
[code=auto:0]

any suggestions?
thanks again

#7 kir10s

kir10s
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 May 2006 - 06:30 PM


1)You might check what keys you have set within your table. You may be trying to write a record, and since the key is already being utilized, it is kicking the update out.


2)I might suggest that you write your query in a sql string, then echo the string so you can see it. Then copy and paste it into the sql window of your php admin panel. It gives you more information as to what the error may be... such as

I would imagine there is a way to echo the error within the code. Maybe someone else can comment on that



#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 06:59 PM

<input type=\"hidden\" name=\"ud_id\" value=\" $id \">

Why do you surround $id with spaces??? Then that's why it doesn't update, because there's no record where id = '<space>xx<space> '.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 elginwick

elginwick
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 May 2006 - 07:05 PM

[!--quoteo(post=374389:date=May 16 2006, 02:59 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ May 16 2006, 02:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<input type=\"hidden\" name=\"ud_id\" value=\" $id \">

Why do you surround $id with spaces??? Then that's why it doesn't update, because there's no record where id = '<space>xx<space> '.
[/quote]


Thanks for all your help everyone.
It was just me being stupid

the problem was that on the first page i used
<input type=\"hidden\" name=\"ud_id\" value=\" $id \">

and i needed to use
<input type=\"hidden\" name=\"ud_id\" value=\" $_POST[sel_id] \">

thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users