Jump to content

Update Problem


olilish
 Share

Recommended Posts

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

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

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

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

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...