Jump to content

Archived

This topic is now archived and is closed to further replies.

elginwick

updating mysql with php

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
besides the column name "session", it's UPDATE SET, not UPDATE WHILE

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
what you should do now is echo the query string.
[code]$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();[/code]

Share this post


Link to post
Share on other sites
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]
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]


then the second page that the form posts to is

[code]
<?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]

any suggestions?
thanks again

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
[code]<input type=\"hidden\" name=\"ud_id\" value=\" $id \">[/code]

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> '.

Share this post


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

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.