Jump to content

Update data in MySQL by PDO


Sigmahokies

Recommended Posts

Hello everyone

I tried to create update site with PDO method, but not success. Can you help? I followed what instruct from w3school, but none of them are work.

<?php

include("connection.php");

if($_GET['update']) {

$data = $_GET['update'];

$prepare = "SELECT * FROM Members WHERE ID = $data";

$sql = $GaryDB->prepare($prepare);

$sql->execute();

if($row = $sql->fetch()) {

$form = "<tr><td>ID</td><td><input type='text' name='updated1' value='".$row['ID']."' readonly></td></tr>
         <tr><td>First Name:</td><td><input type='text' name='updated2' value='".$row['FirstName']."'></td></tr>
         <tr><td>Last name:</td><td><input type='text' name='updated3' value='".$row['LastName']."'></td></tr>
         <tr><td>Address:</td><td><input type='text' name='updated4' value='".$row['Address']."'></td></tr>
         <tr><td>State:</td><td><input type='text' name='updated5' value='".$row['State']."'></td></tr>
         <tr><td>Zip:</td><td><input type='text' name='updated6' value='".$row['Zip']."'></td></tr>
         <tr><td colspan='2' style='text-align: center'><input type='submit' name='submit' value='UPDATE'></td></tr>";

}


} else {
$form = "<tr><td>No ID in URL</td></tr>";
}

if(isset($_POST['submit'])) {

$id = $_POST['update1'];
$first = $_POST['update2'];
$last = $_POST['update3'];
$address = $_POST['update4'];
$state = $_POST['update5'];
$zip = $_POST['update6'];

$updatesql = $GaryDB->prepare("UPDATE Members SET FirstName = :first, 
LastName = :last, 
Address = :address, 
State = :state, 
Zip = :zip 
WHERE ID = :id");

$updatesql->bindParam(':id', $id, PDO::PARAM_INT);
$updatesql->bindParam(':first', $first, PDO::PARAM_STR);
$updatesql->bindParam(':last', $last, PDO::PARAM_STR);
$updatesql->bindParam(':address', $address, PDO::PARAM_STR);
$updatesql->bindParam(':state', $state, PDO::PARAM_STR);
$updatesql->bindParam(':zip', $zip, PDO::PARAM_STR);

$updatesql->execute();

if($updatesql->rowCount()) {
$success = "<script>alert('success update')</script>";
} else {
$success = "<script>alert('Not success update')</script>";
}
}

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Update site</title>
<link href="red-sigma.png" rel="icon">
<link href="default.css" rel="stylesheet" type="text/css">
</head>
<body>
<a href='demonstration.php'>return to demonstration</a><br />
<h2>Welcome to Gary Taylor's Demonstration in update in database</h2>
<?php echo $data ?>
<?php echo $id, $first, $last, $address, $state, $zip; ?>
<form action="update.php" method="post">
<table>
<?php echo $form ?>
<?php echo $success ?>
</table>
</form>
</body>
</html>

 

Edited by Sigmahokies
Link to comment
Share on other sites

Where do you think it broke and what are the error messages, if any?  
 

EDIT:

Where did you initiate the PDO object?

Edited by niche
Link to comment
Share on other sites

Yes, Select is successful. I tested it by echo quote SELECT before go to form. Problem is it won't get in database as updated.

I should checked step by one, like update one, then next two, then next three...I'm trying to get used with PDO instead of mysqli because it's getting old. I can tell PDO is more easier than mysqli. 

Link to comment
Share on other sites

Does that Id exist?

if so, are you actually changing anything. rowCount() returns 0 if you don’t actually change anything.

odd, nothing’s happening and no errors. 

is error reporting on?

Edited by niche
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
×
×
  • Create New...