Jump to content

Update


ryan1234

Recommended Posts

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.

Link to comment
Share on other sites

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 by mrMarcus
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by PFMaBiSmAd
Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.