Good evening.I am a beginner in PHP.
I am currently creating a student registration system, but I am not able to update student information well.
I have created a php based on this site, but currently I can only update the name, but I cannot update any other information.
https://noumenon-th.net/programming/2016/01/20/mysql-3/
From the page where the student information is displayed, when you click Edit, it becomes an edit screen. On the edit screen, we would like to put the information pulled from the previous page into Value and update the database information by putting the changed characters in Value.
Currently, we are making trial and error, so we only update the name and gender.
I tried UPDATE studentinfo SET Name=?,Gender=?WHERE id=?
and UPDATE studentinfo SET Name=:Name,Gender=?
on line 49 of update2sample.php, but it also didn't work.
"Also, I am trying various tasks for the test, but even though the database has not been updated, it says ""update completed"", so please let me know about the error description."
This is my first question, so I'm sure there are some questions, but I appreciate your help.
Currently, the name can be updated, but no other content can be updated.
Currently, I am trying to update my name and gender as I am in the middle.
Show below in SELECT statement
<div class="button">
<form action="update1sample.php" method="post">
<input type="hidden" name="id" value="<?=$row['id']?>">">
<input type="hidden" name="Name" value="<?=htmlspecialchars($row['Name'],ENT_QUOTES,'UTF-8')?>">
<input type="hidden" name="Gender" value="<?=htmlspecialchars($row['Gender'],ENT_QUOTES,'UTF-8')?>">
<input type="submit" value="edit">
</form>
</div>
Writing updatesample1.php
require_once("db_studentinfo.php");
$mysqli=db_connect();
if(empty($_POST)){
echo "<a href='update1.php'>update1.php</a>←Please visit this page";
exit();
} else {
if(!isset($_POST['id'])||!is_numeric($_POST['id']){
echo "ID Error";
exit();
} else {
// Prepared statements
$stmt=$mysqli->prepare("SELECT* FROM studentinfo WHERE id=?");
if($stmt){
// Set the actual value for the placeholder
$stmt->bind_param('i', $id, $Name, $Gender);
$id = $_POST ['id'];
$Name = $_POST ['Name'];
$Gender=$_POST ['Gender'];
// Run Query
$stmt->execute();
// Binding Result Variables
$stmt->bind_result($id,$Name,$Gender);
// acquisition of value
$stmt->fetch();
// Disconnect statement
$stmt->close();
} else {
echo$mysqli->errno.$mysqli->error;
}
}
}
<body>
<h1>Change Screen</h1>
<p>Please enter changes</p>
<form action="update2sample.php" method="post">
<p>Name:<input type="text" name="Name" placeholder="value="<?=htmlspecialchars($Name,ENT_QUOTES,'UTF-8'?>">>/P>
<p>Gender: <input type="text" name="Gender" placeholder="value="<?=htmlspecialchars($Gender,ENT_QUOTES,'UTF-8'?>">/P>
<input type="hidden" name="id" value="<?=$id?>">">
<input type="submit" value="Change">
</form>
</body>
Writing updatesample2.php
header("Content-type:text/html; charset=utf-8");
require_once("db_studentinfo.php");
$mysqli=db_connect();
if(empty($_POST)){
echo "<a href='update1.php'>update1.php</a>←Please visit this page";
exit();
} else {
// Name input check
if(!isset($_POST['Name'])||$_POST['Name']===""){
$errors['Name']="No name entered.";
}
if(count($errors)===0){
// Prepared statements
$stmt=$mysqli->prepare("UPDATE studentinfo SET Name=?WHERE id=?");
if($stmt){
// Set the actual value for the placeholder
$stmt->bind_param('si',$Name,$id);
$Name = $_POST ['Name'];
$id = $_POST ['id'];
$Gender=$_POST ['Gender'];
// Run Query
$stmt->execute();
// Disconnect statement
$stmt->close();
} else {
echo$mysqli->errno.$mysqli->error;
}
}
}
<h1>Change Screen</h1>
<?php if(count($errors)===0):?>
<p>Changes complete.</p>
<?phpelseif(count($errors)>0):?>
<?php
foreach($errors as$value){
echo "<p>".$value."</p>";
}
?>
<?php endif;?>
</body>
I wrote $Gender
on line 49 of update2sample.php
and tried UPDATE studentinfo SET Name=?,Gender=?WHERE id=?", "UPDATE studentinfo SET Name=:Name,Gender=?
and so on.
updatesample1.php
if($stmt){
// Set the actual value for the placeholder
$stmt->bind_param('i',$id);
// $Name=$_POST ['Name']; <- This line is not required
$id = $_POST ['id'];
// $Gender=$_POST ['Gender']; <- This line is not required
updatesample1.php
UPDATE studentinfo SET Name=?,Gender=?WHERE id=?
.$Gender
, $id
to the parameters of the UPDATE statement in bind_param()
.ssi
. // Add Gender to SET clause
$stmt=$mysqli->prepare("UPDATE studentinfo SET Name=?,Gender=?WHERE id=?");
if($stmt){
// Bind variables to SQL parameters. Bind the following in the order in which ? appears.
// US>1. Value: $Name Type: s (string)
// 2. Value: $Gender Type: s (string)
// 3. Value: $id Type: i (integer)
$stmt->bind_param('ssi', $Name, $Gender, $id);
I think it's like this.
For your information, I think it will be faster to understand the relationship between SQL and bind_param() by looking at the manual.https://www.php.net/manual/ja/mysqli-stmt.bind-param.php
635 Uncaught (inpromise) Error on Electron: An object could not be cloned
624 GDB gets version error when attempting to debug with the Presense SDK (IDE)
575 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
929 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.