I stumble when I update the database from PHP.

Asked 2 years ago, Updated 2 years ago, 138 views

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>

php mysql phpmyadmin

2022-09-30 19:25

2 Answers

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 $idNormally, no record has a value of id $id, so doing this should "look like nothing is happening."

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.)


2022-09-30 19:25

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!


2022-09-30 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.