I'm a beginner in PHP.
View the databases associated with each id in detail.php and update those values in dataupdate.php.
I would like to do this, but it doesn't work.
The detail.php is as follows (just in case, it worked well)
<?php
// Connect to DB
$pdo=new PDO('mysql:dbname=otoiawase; host=localhost', 'root', '');
// Specify character code
$stmt = $pdo->query('SET NAMES utf8');
// Create Data Registration SQL
$stmt=$pdo->prepare("SELECT* FROM address_table WHERE id=:id");
$stmt->bindParam(':id',$_GET['id']);
// Run SQL
$flag = $stmt->execute();
// Error handling
if($flag==false){
echo "SQL Error";
} else {
$result=$stmt->fetch();
$name = array_column($result, 'name');
// // Remove email array from $result
$email=array_column($result, 'email');
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<metacharset="UTF-8">
<title>Document</title>
</style>
</head>
<body>
<form action="dataupdate.php"method="post">
<input type="text" name="id" value="<?phpecho$result['id'];?>">
<input type="text" name="name" value="<?phpecho$result['name'];?>">
<input type="text" name="email" value="<?php echo$result['email'];?>">
<input type="submit" value="Update">
</form>
</body>
</html>
I want to update with dataupdate.php from here, but it doesn't work...
The dataupdate.php is as follows.I was not sure what caused it, so I would appreciate it if you could point it out.
<?php
$id = $_POST ['id'];
$name = $_POST ['name'];
$email=$_POST ['email'];
// Connect to DB
$pdo=new PDO('mysql:dbname=otoiawase; host=localhost', 'root', '');
// Specify character code
$stmt = $pdo->query('SET NAMES utf8');
// Create Data Registration SQL
$update=$pdo->prepare("UPDATE address_table SET name=:name, email=:email WHERE id=:id");
$update->bindValue(':name', '$name');
$update->bindValue(':email', '$email');
$update->bindValue(':id', '$id');
// Run SQL
$flag = $update->execute();
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<metacharset="UTF-8">
<title>Document</title>
</head>
<body>
The data has been updated.
</body>
</html>
As I wrote in the comment, I don't know how it works at all, so there may be more, but at least these three lines need to be corrected.
$update-> bindValue(':name', '$name');
$update->bindValue(':email', '$email');
$update->bindValue(':id', '$id');
PHP is a string enclosed by a single quotation mark '
that is not subject to variable expansion, even if there is a letter like $
inside.Therefore, your code must contain the value of the name
column of the record whose value in the id
column is $
i
d
, so doing this should "look like nothing is happening."
Normally, no record has a value of id
$id
It doesn't have to be in quotation marks, so if I were to write it, I'd say:
$update->bindValue(':name',$name);
$update->bindValue(':email',$email);
$update->bindValue(':id',$id);
If you still have something to do with this correction, please add it to your question (note what you wrote in the comment) or let me know as a comment to this answer.
Note that although this may not be directly related to this issue, remember to escape the PHP variable values appropriately when outputting them in HTML.
<input type="text" name="id" value="<?=htmlspecialchars($result['id'])?>">
<input type="text" name="name" value="<?=htmlspecialchars($result['name'])?>">
<input type="text" name="email" value="<?=htmlspecialchars($result['email'])?>">
I understand that it's a practice code that I'm getting into, but I'm in trouble if you get used to it from the beginning and think, "It's disgusting to output variable values into HTML without escaping."(I think it would be better if we could learn more about checking errors and sanitizing validation of input values.Unfortunately, there are many PHP introductory articles that are not mentioned at all.)
Thank you for pointing it out!I'm very sorry for the bad way you asked!
The thing that didn't work was, "There are no syntax errors or other errors in the browser, but the data is not updated."
As you pointed out,
$update->bindValue(':name',$name);
$update->bindValue(':email',$email);
$update->bindValue(':id',$id);
The variable portion ($name, $email, $id) of was enclosed with '', and I couldn't find the mistake...
It's been 2 days since I studied PHP, so I'm sorry for asking such a rudimentary question!
After correcting it, the code now works properly.Thank you!
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Understanding How to Configure Google API Key
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.