happypete Posted January 15, 2010 Share Posted January 15, 2010 Hi, I can do this with MySQl but am now trying with PDO but it wont update all the rows. I assume i need a 'foreach' somewhere but can't work it out. It only updates where id=3 HTML form and PHP update script included: admin5.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="css/default.css" type="text/css" /> <title>Test Site</title> </head> <body> <h1>Test Site</h1> <form method="post" action="5update.php" enctype="multipart/form-data"> <fieldset> <label>Rates <div class="clear"></div> <input type="hidden" name="id" value="1" /> <input class="mediuminput" type="text" name="dates" value="ski seasons" /> <input class="smallinput" type="text" name="night" value="u$s 34" /> <input class="smallinput" type="text" name="week" value="u$s 345" /> <input class="smallinput" type="text" name="month"value="u$s 1234" /> <input class="smallinput" type="text" name="min" value="3 nights" /> <input class="smallinput" type="text" name="rank" value="2" /> </form> </label> <input type="hidden" name="id" value="2" /> <input class="mediuminput" type="text" name="dates" value="ewrew" /> <input class="smallinput" type="text" name="night" value="wer" /> <input class="smallinput" type="text" name="week" value="wer" /> <input class="smallinput" type="text" name="month"value="ewqr" /> <input class="smallinput" type="text" name="min" value="weqr" /> <input class="smallinput" type="text" name="rank" value="3" /> </form> </label> <input type="hidden" name="id" value="3" /> <input class="mediuminput" type="text" name="dates" value="123dds" /> <input class="smallinput" type="text" name="night" value="asw" /> <input class="smallinput" type="text" name="week" value="as" /> <input class="smallinput" type="text" name="month"value="as" /> <input class="smallinput" type="text" name="min" value="as" /> <input class="smallinput" type="text" name="rank" value="4" /> </form> </label> <div class="clear"></div> <input class="green" type="submit" name="submit" value="Update Entry" /> </fieldset> </form> </body> </html> 5update.php <?php // Include the necessary files include_once 'inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Check if coming from a POST command and if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Update Entry') { // update data into the database if(!empty($_POST['id'])) //$id = $_POST['id']; $sql = "UPDATE rates SET dates=?, night=?, week=?, month=?, min=?, rank=? WHERE id=?"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['dates'], $_POST['night'], $_POST['week'], $_POST['month'], $_POST['min'], $_POST['rank'], $_POST['id'], ) ); $stmt->closeCursor(); // once updated return to admin page header('Location: admin5.php'); exit; } else { // If nothing happend send back to admin page header('Location: admin5.php'); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188603-it-wont-update-all-rows/ Share on other sites More sharing options...
Maq Posted January 15, 2010 Share Posted January 15, 2010 You can make your id fields an array "id[]" so when you pass it to the SQL statement you can use: WHERE id IN(?); and implode the id array in the PDO execute() call. Quote Link to comment https://forums.phpfreaks.com/topic/188603-it-wont-update-all-rows/#findComment-995802 Share on other sites More sharing options...
happypete Posted January 15, 2010 Author Share Posted January 15, 2010 Thank Maq, but could you be a bit more specific please, I don't understand you answer. thanks Quote Link to comment https://forums.phpfreaks.com/topic/188603-it-wont-update-all-rows/#findComment-995830 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.