Reputation: 146
I'v been beating my head in the wall for the past 10 hours trying to get a simple form to update sql table data by id. I'm sure its something simple right infont of my face.
If someone can slap my with the stupid stick id appreciate it! haha
Goal- To be able to fill out the form below and update a row via id in sql.
<?php
$dsn = 'mysql:dbname=xxxx;host=xxxxx';
$user = 'xxxxx';
$pass = 'xxxxx';
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo '<form method="post" action="">
ID: <input type="text" name="a" /><br>
Program: <input type="text" name="b" /><br>
Description: <textarea row="6" cols="50" name="c"></textarea><br>
Cost: <input type="text" name="d"><br>
Source: <input type="text" name="e"><br>
URL: <input type="text" name="f"><br>
<input type="submit" value="Add Link" />
</form>';
}
try {
$dbh = new PDO($dsn, $user, $pass);
$stmt = $dbh->prepare('UPDATE links SET Program = :program, Descr = :descr, Cost = :cost, Source = :source, url = :url, WHERE Id = :id');
//Im sure $stmt->bindParam can be done via array but im just tring to get the stupid thing to update at the moment
$stmt->bindParam(":id", $_POST["a"]);
$stmt->bindParam(":program", $_POST["b"]);
$stmt->bindParam(":descr", $_POST["c"]);
$stmt->bindParam(":cost", $_POST["d"]);
$stmt->bindParam(":source", $_POST["e"]);
$stmt->bindParam(":url", $_POST["f"]);
$stmt->execute();
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());}
$dbh = null;
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Thanks for the help! JR
Upvotes: 1
Views: 54
Reputation: 270677
Your prepared statement is not succeeding but PDO has not been configured to throw an exception. Its default behavior is to silently ignore errors.
Review the PDO documentation on error handling.
try {
// The constructor *will* throw an exception on error
$dbh = new PDO($dsn, $user, $pass);
// But subsequent stuff won't unless configured to do so...
// Tell PDO to throw exceptions
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// etc...
The source of your error is an errant comma before the WHERE
clause. (The only error I can spot visually, anyway).
$stmt = $dbh->prepare('UPDATE links SET Program = :program, Descr = :descr, Cost = :cost, Source = :source, url = :url, WHERE Id = :id');
// -----------------------------------------------------------------------------------------------------------------^^^^^
Upvotes: 2