Jump to content

Recommended Posts

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/188603-it-wont-update-all-rows/
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.