olilish Posted January 26, 2010 Share Posted January 26, 2010 Hi Guys, Hope someone can help, I have a problem updating my database.The idea is that I can add new records and then edit them using UPDATE. However even though I do not get any errors from my host the record is not being updated. This is my code (this comes directly after my connection info):------------------------------------------------------------------------$id = $_GET['id'];$h1=$_POST['h1'];$body=$_POST['body'];$h2=$_POST['h2'];$a=$_POST['a']; $sql = ("UPDATE pages SET body='$body', h1='$h1', WHERE id=$id"); echo "Thank you! Information updated.";----------------------------------------------------------------------I have also tried this without any joy:mysql_query("UPDATE pages SET body = '" . $_POST['body'] . "' WHERE id = " . $ID);---------------------------------------------------------------------Any help would be great!Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 You're not running the first query, you're just building a SQL string. You need to use mysql_query to run it. You also have an extra comma in the query. You can use mysql_error to print out the error from MySQL if the query failed:http://www.php.net/manual/en/function.mysql-error.php Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 Thanks, I have made the changes you suggest:mysql_query("UPDATE pages SET body='$body', h1='$h1' WHERE id=$id");but still no update.anything i'm doing wrong?thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 Check for errors:mysql_query("UPDATE pages SET body='$body', h1='$h1' WHERE id=$id") or exit(mysql_error()); Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 this is what i get:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 Print out the query so that you can see what values you're sending. Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 i'm sorry, i don't know what that means. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 echo "UPDATE pages SET body='$body', h1='$h1' WHERE id=$id"; Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 It prints this:UPDATE pages SET body='aaaa', h1='' WHERE id=Thank you! Information updated.So does this mean that I am passing 'aaaa' (which is what i typed as my update. And I didn't type anything for h2, so there is nothing there. But WHERE id= ...and there is nothing there, that should show my id number? I have specified $id = $_GET['id'];? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 It sounds like $_GET['id'] isn't set. Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 Shouldn't i just have to go:$id = $_GET['id'];?Which is what I have in my code. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 Assuming that there is actually something in $_GET['id'], and that you don't overwrite $id later, then yeah. You can use this to see what's in $_GET:print_r($_GET); Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 I added that and i get this:UPDATE pages SET body='my update', h1='' WHERE id=Array ( ) Thank you! Information updated. What does this mean? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 If print_r($_GET) produces this:Array ( )Then $_GET is empty. Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 Which is odd, as in my other scripts when I add print_r($_GET); I am getting, for example:Array ( [id] => 33 )Any ideas why on others but not this?:<?php$con = mysql_connect('xxxxxxxxxxxxxxxx','xxxxxxxxxxxx','xxxxxxxx');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("a4411005_mycms");$ID = $_GET['id'];$h1=$_POST['h1'];$body=$_POST['body'];$h2=$_POST['h2'];$a=$_POST['a'];mysql_query("UPDATE pages SET body='$body', h1='$h1' WHERE id=" . $ID);echo "UPDATE pages SET body='$body', h1='$h1' WHERE id=$ID"; print_r($_GET); echo "Thank you! Information updated.";mysql_close($con);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 The only reason that $_GET would be empty is that you are actually not passing anything through the URL. In order for $_GET['id'] to be set, you must pass a variable called id in the URL, e.g.:page.php?id=xxxIf $_GET does not have anything in it, the single reason is that you are not passing anything on the URL. If you were passing URL variables, then they would be in $_GET. Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 Understood,Maybe my problem is something more fundamental.Do I need to have the php on the same page as my html form that updates the record? Can a variable only be passed when a link is clicked? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 You can add variables to any URL, e.g. the action of a form. Do I need to have the php on the same page as my html form that updates the record?No, but it's generally more useful to have the PHP in the same place as the form that it processes. Link to comment Share on other sites More sharing options...
olilish Posted January 26, 2010 Author Share Posted January 26, 2010 Up to this point i have only ever used a form action to run a script seperate from that html form.how do i run the php in the same html page as well as passing the variable in the action of the form?i tried this:<form method="post" action="edit.php?="id=$ID>but it only seemed to pass the edit.php into my $id. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2010 Share Posted January 27, 2010 You still submit the form to a page, but you can just have that page be the same one and have some code which checks if the form was submitted before trying to process it. You can use isset to check that, if your form submits using post and you have a submit button with a name of "submit", then using this will tell you if the form was submitted, or if they're just loading the page for the first time:if (isset($_POST['submit']))For your action, you can do something like this:<form method="post" action="edit.php?id=<?php echo $id; ?>">If you want to be able to rename your files without changing the form action, you can have PHP fill in the filename:<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $id; ?>">You can also put your id in a hidden form element and then get it from $_POST instead of $_GET:<input type="hidden" name="id" value="<?php echo $id; ?>"> Link to comment Share on other sites More sharing options...
olilish Posted January 27, 2010 Author Share Posted January 27, 2010 So can I just have my form html and then start my php at the bottom of the same page? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2010 Share Posted January 27, 2010 It's generally best to have all PHP code at the top, so that your PHP code is finished by the time you need to figure out what HTML code to display, or whether you need to redirect, etc. Link to comment Share on other sites More sharing options...
olilish Posted January 27, 2010 Author Share Posted January 27, 2010 So could I do this?----------------------------------<html><body><?php$con = mysql_connect('xxx','xxx_x','xxx');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("xxx_mycms");$ID = $_GET['id'];;$h1=$_POST['h1'];$body=$_POST['body'];$h2=$_POST['h2'];$a=$_POST['a'];mysql_query("UPDATE pages SET body='$body', h1='$h1' WHERE id=" . $ID);echo "Thank you! Information updated.";mysql_close($con);?><form method="post" action="edit.php?id=<?php echo $id; ?>"><textarea name="body"> </textarea> <input name="update" type="submit" id="update" value="Update"></form></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2010 Share Posted January 28, 2010 You can do something like that, but you still need to check if the form was submitted before you try to process it. If you start the HTML before you start the PHP you also won't be able to use PHP to redirect. Link to comment Share on other sites More sharing options...
olilish Posted January 28, 2010 Author Share Posted January 28, 2010 Im sorry, but what do you mean by 'you need to check if the form was submitted before you process it'? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.