ryan1234 Posted December 10, 2012 Share Posted December 10, 2012 When a submit button is clicked it sends it to a page which contains the code below. It also sends 'id'. I've been trying to update a table in the database using the code below but it doesn't seem to be working. I think it's something to do with the line: WHERE id = " . $row_id ; $row_id = $_GET['id']; $sql = "UPDATE newsitems SET headline = :h, author = :a, story = :s, image = :i WHERE id = " . $row_id ; $query = $handle->prepare($sql); Any ideas? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/ Share on other sites More sharing options...
Stooney Posted December 10, 2012 Share Posted December 10, 2012 Try: $row_id = $_GET['id']; $sql = "UPDATE newsitems SET headline = :h, author = :a, story = :s, image = :i WHERE id = '$row_id'" ; $query = $handle->prepare($sql); Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398547 Share on other sites More sharing options...
Beeeeney Posted December 10, 2012 Share Posted December 10, 2012 Do a var_dump() on the $_GET array. Are you using method="get" on your form? Also, "doesn't seem to be working" is a bit vague. What exactly is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398548 Share on other sites More sharing options...
ryan1234 Posted December 10, 2012 Author Share Posted December 10, 2012 Stooney tried what you said, didn't work. It sends the id like this: update.php?id=......... It's not updating the table, not too sure the exact problem. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398550 Share on other sites More sharing options...
mrMarcus Posted December 10, 2012 Share Posted December 10, 2012 (edited) Post your form please. EDIT: When you say it "sends the id like this: update.php?id=.........." does id= actually have an id appended to it as a value? E.g. update.php?id=12345 Posting a series of dots is not helpful. Do you have an `id` column in the table? Are you sure? Edited December 10, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398551 Share on other sites More sharing options...
ryan1234 Posted December 10, 2012 Author Share Posted December 10, 2012 <form action="editphp.php?id=<?php echo $row['id']; ?>" method="POST"> Headline: <input type="text" name="headline" class="editblog" id="editheadline" value="<?php print $results['headline']; ?>"><br> Author: <input type="text" name="author" value="<?php print $results['author']; ?>"><br> Image (URL): <input type="text" name="image" value="<?php print $results['image']; ?>"><br> Story: <br><textarea cols="60" rows="20" id="editstory"><?php print $results['story']; ?></textarea><br> <input type="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398552 Share on other sites More sharing options...
Beeeeney Posted December 10, 2012 Share Posted December 10, 2012 You have method="post" and on the next page, you're trying to get it with $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398555 Share on other sites More sharing options...
mrMarcus Posted December 10, 2012 Share Posted December 10, 2012 You have method="post" and on the next page, you're trying to get it with $_GET. $_GET['id'] will still be retrievable. @ryan1234 - if you view the page source of your form is id= populated with the expected value/id ($row['id'])? Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398556 Share on other sites More sharing options...
ryan1234 Posted December 10, 2012 Author Share Posted December 10, 2012 Not one it is redirected back to the same page when the button is clicked. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398560 Share on other sites More sharing options...
mrMarcus Posted December 10, 2012 Share Posted December 10, 2012 Not one it is redirected back to the same page when the button is clicked. Help us/me to help you. I asked you if when you view the page course of your form, does your form action have the desired value for the id parameter? E.g <form action="editphp.php?id=<?php echo $row['id']; ?>" method="POST"> // is $row['id'] supplying an expected value when you view the browser source code? Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398561 Share on other sites More sharing options...
ryan1234 Posted December 11, 2012 Author Share Posted December 11, 2012 Sorry, yeah it does. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398675 Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2012 Share Posted December 11, 2012 (edited) This is secondary to the problem you are having, but by putting the $row_id variable directly into the query being prepared, you are allowing sql injection, not preventing it. One of the main points of using prepared query statements is to prevent sql injection. You would put a place holder into the query for the id value, then supply the actual value at the time the query is executed. Edit: ^^^ Which I had already posted at the end of your last thread - http://forums.phpfreaks.com/topic/271784-show-row/#entry1398388 Edited December 11, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398677 Share on other sites More sharing options...
ryan1234 Posted December 11, 2012 Author Share Posted December 11, 2012 $sql = "UPDATE newsitems SET headline = :h, author = :a, story = :s, image = :i WHERE id = :r" ; $query = $handle->prepare($sql); $params = array(":h" => $_POST['headline'], ":a" => $_POST['author'], ":s" => $_POST['story'], ":i" => $_POST['image'], ":r" => $row_id); $query->execute($params); Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398719 Share on other sites More sharing options...
ryan1234 Posted December 11, 2012 Author Share Posted December 11, 2012 Used error info and I get this message: Array ( [0] => 00000 [1] => [2] => ) Error info: 1 Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398720 Share on other sites More sharing options...
ryan1234 Posted December 11, 2012 Author Share Posted December 11, 2012 Solved, I wasn't posting the id properly. Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/271824-update/#findComment-1398723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.