vampke Posted May 3, 2007 Share Posted May 3, 2007 Hi guys, I'm trying to get this script to get data from a mysql table and put it in html. So far so good. Now I want to be able to update this data and put the changes back in the mysql table. Problems here. The script I wrote for the update: $update = "UPDATE aaarticle SET id='$_POST[id]', date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'"; mysql_query($update,$conn) or die(mysql_error()); This does not seem to be making any changes to the table though. What could be wrong? Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/ Share on other sites More sharing options...
mmarif4u Posted May 3, 2007 Share Posted May 3, 2007 try this: $update = "UPDATE aaarticle SET id='$_POST['id']', date='$_POST['date']', spec='$_POST['spec']', title='$_POST['title']', text='$_POST['text']' WHERE id= '$_POST['id']'"; mysql_query($update,$conn) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244115 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 Thanks for the quick reply. Now I get Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in pathto/update.php on line 6 Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244120 Share on other sites More sharing options...
mmarif4u Posted May 3, 2007 Share Posted May 3, 2007 ok For easy understanding: $id='$_POST['id']; $date='$_POST['date']; $spec='$_POST['spec']; $title='$_POST['title']; $text='$_POST['text']; $update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'"; mysql_query($update,$conn) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244124 Share on other sites More sharing options...
jitesh Posted May 3, 2007 Share Posted May 3, 2007 $update = "UPDATE aaarticle SET id='".$_POST[id]."', date='".$_POST[date]."', spec='".$_POST[spec]."', title='".$_POST[title]."', text='".$_POST[text]."' WHERE id= '".$_POST[id]."'"; Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244126 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 $update = "UPDATE aaarticle SET id='".$_POST[id]."', date='".$_POST[date]."', spec='".$_POST[spec]."', title='".$_POST[title]."', text='".$_POST[text]."' WHERE id= '".$_POST[id]."'"; same problem Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244140 Share on other sites More sharing options...
yzerman Posted May 3, 2007 Share Posted May 3, 2007 Try this: <?PHP //make sure these variables are defined in $conn and that you have selected a database. $hostname = 'localhost'; $dbuser = 'dbuser'; $dbpass = 'dbpass'; $dbselect = 'dbname'; $conn = mysql_connect($hostname, $dbuser, $dbpass); //lets make sure were connected with $conn - then select our database if ($conn) { //if there were no connection issues mysql_select_db($dbselect); //select the database //ok now it looks like your trying to update the ID - and if I was a betting man - the id is auto_increment, which would cause this query to fail // so we replace this: $update = "UPDATE aaarticle SET id='$_POST[id]', date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'"; // with $update = "UPDATE aaarticle SET date='$_POST[date]', spec='$_POST[spec]', title='$_POST[title]', text='$_POST[text]' WHERE id= '$_POST[id]'"; $result = mysql_query($update); // run the query if ($result) { //if there was a result //do something } else { //the query failed echo mysql_error(); } } else { //connection issue echo 'there was an issue connecting to the server, check that you have properly identified the credentials.'; } ?> Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244142 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 ok For easy understanding: $id='$_POST['id']; $date='$_POST['date']; $spec='$_POST['spec']; $title='$_POST['title']; $text='$_POST['text']; $update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'"; mysql_query($update,$conn) or die(mysql_error()); Parse error: syntax error, unexpected T_STRING in pathto/update.php on line 6 line 6 = $id='$_POST['id']'; I added a ' after each line before ; I think you forgot that in your reply, right? Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244144 Share on other sites More sharing options...
mmarif4u Posted May 3, 2007 Share Posted May 3, 2007 try this: $id=$_POST['id']; $date=$_POST['date']; $spec=$_POST['spec']; $title=$_POST['title']; $text=$_POST['text']; $update = "UPDATE aaarticle SET id='$id', date='$date', spec='$spec', title='$title', text='$text' WHERE id= '$id'"; mysql_query($update,$conn) or die(mysql_error()); Sorry for that. Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244146 Share on other sites More sharing options...
yzerman Posted May 3, 2007 Share Posted May 3, 2007 no I understand your defining those but with what you just posted your going to end up with errors you do not need to enclose globals in single quotes (') to define them - so they should be $id = $_POST['id']; and NOT $id = '$_POST['id']; //will cause the error Parse error: syntax error, unexpected T_STRING in path/to/blah.php on line whatever Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244147 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 Try this: /code/ I tried it, but changed 1 line: if ($result) { echo 'succes!'; } The script returns succes! but it still fails to update the table :'-O Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244149 Share on other sites More sharing options...
mmarif4u Posted May 3, 2007 Share Posted May 3, 2007 show ur update code.. Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244150 Share on other sites More sharing options...
yzerman Posted May 3, 2007 Share Posted May 3, 2007 I think your main problem is that you were trying to update the id - when there is no need to update the id - that should remain as it is- the contents for that id are the only things that should change. You can do it either way thought, $update = "UPDATE article SET date='$_POST[date]'"; //this way works as long as its written as $_POST[date] and not $_POST['date'] or $update = "UPDATE article SET date='$date'"; Its all in how you prefer to do it. Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244151 Share on other sites More sharing options...
yzerman Posted May 3, 2007 Share Posted May 3, 2007 vampke, ensure your not trying to set database values that are automatically updated. I.E. time, date, auto_increment id's, etc. Also - if you have access to run the update via phpMyAdmin, do so and see what errors you are getting. Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244153 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 vampke, ensure your not trying to set database values that are automatically updated. I.E. time, date, auto_increment id's, etc. Also - if you have access to run the update via phpMyAdmin, do so and see what errors you are getting. Thank you very much everybody that was trying to help me! I am VERY sorry and ashamed to say that I have sold the problem myself.... I misnamed the id box in the html code.... :-[ :-[ :-[ Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244154 Share on other sites More sharing options...
mmarif4u Posted May 3, 2007 Share Posted May 3, 2007 its ok, some time it happens. Please mark the post as solved. Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244170 Share on other sites More sharing options...
vampke Posted May 3, 2007 Author Share Posted May 3, 2007 ok, have done so Link to comment https://forums.phpfreaks.com/topic/49768-solved-mysql-update-doesnt-work/#findComment-244174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.