为了账号安全,请及时绑定邮箱和手机立即绑定

使用预准备语句更新表

使用预准备语句更新表

PHP
达令说 2022-09-30 16:19:45
我正在尝试更新我的卡片表。用户选择“更新”按钮后,它会将他重定向到更新页面,在那里他可以看到和修改他的数据。问题是,输入字段不会加载他的数据,并且由于某种原因也无法更新它们。这是第一页上的按钮:<a href="update.php?id=<?php echo $record['id']; ?>" class="btn btn-succes" role="button">Edit</a>这是更新页面:<?phpsession_start();define('DB_SERVER', 'localhost');define('DB_USERNAME', 'root');define('DB_PASSWORD', '');define('DB_NAME', 'reg');/* Attempt to connect to MySQL database */$mysqli = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);// Check connectionif($mysqli === false){    die("HIBA: Nem sikerült csatlakozni. " . mysqli_connect_error());}$stmt = $mysqli -> prepare('UPDATE cards SET name=?, phone=?, phone2=?, email=?, zipcode=?, address=?, job=?, description=?, visibility=?, confirmed=?, userid=?  WHERE id = ?');if (    $stmt &&    $stmt->bind_param('ssssisssiiii', $name, $phone, $phone2, $email, $zipcode, $address, $job, $description, $visibility, $confirmed, $userid, $id)    &&    $stmt -> execute() &&    $result = mysqli_query($mysqli,"SELECT * FROM cards WHERE id='$id'") &&    $result = $stmt -> get_result() ) {        $id = $row['id'];        $name = $row['name'];        $phone = $row['phone'];        $phone2 = $row['phone2'];        $email = $row['email'];        $zipcode = $row['zipcode'];        $address = $row['address'];        $job = $row['job'];        $description = $row['description'];        $userid = $_SESSION['userid'];        echo 'Updated';    } else {    echo $mysqli -> error;}?> <form action="update.php" method="post"><table cellpadding="10" cellspacing="0" width="500" class="tblSaveForm"><tr class="header"><td colspan="2">Edit Card</td></tr><tr><td><label>Username</label></td><td><input type="text" name="name" class="txtField" value="<?php echo $result['name']; ?>"></tr><tr><td><label>phone</label></td><td><input type="text" name="phone" class="txtField" value="<?php echo $result['phone']; ?>"></td></tr>
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

您缺少将用户输入放入语句中使用的所有变量的代码。并且更新应仅在提交表单时使用,而不是在您最初显示表单时使用。UPDATE


您需要将 放入表单属性中的 URL 中,以便它知道要更新哪个 ID。idaction


<?php

session_start();

define('DB_SERVER', 'localhost');

define('DB_USERNAME', 'root');

define('DB_PASSWORD', '');

define('DB_NAME', 'reg');


/* Attempt to connect to MySQL database */

$mysqli = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);


// Check connection

if($mysqli === false){

    die("HIBA: Nem sikerült csatlakozni. " . mysqli_connect_error());

}


$id = $_GET['id'];


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

    $name = $_POST['name'];

    $phone = $_POST['phone'];

    $phone2 = $_POST['phone2'];

    $email = $_POST['email'];

    $zipcode = $_POST['zipcode'];

    $address = $_POST['address'];

    $job = $_POST['job'];

    $description = $_POST['description'];

    $visibility = $_POST['visibility'];

    $confirmed = $_POST['confirmed'];

    $userid = $_POST['userid'];


    $stmt = $mysqli -> prepare('UPDATE cards SET name=?, phone=?, phone2=?, email=?, zipcode=?, address=?, job=?, description=?, visibility=?, confirmed=?, userid=?  WHERE id = ?');


    if (

        $stmt &&

        $stmt->bind_param('ssssisssiiii', $name, $phone, $phone2, $email, $zipcode, $address, $job, $description, $visibility, $confirmed, $userid, $id) &&

        $stmt -> execute()

        ) {

        echo 'Updated';

    } else {

        echo $mysqli -> error;

    }


$getstmt = $mysql->prepare("SELECT * FROM cards WHERE id= ?");

if ($getstmt and

    $getstmt->bind_param('i', $id) and

    $getstmt->execute() and

    $result = $getstmt->get_result() and

    $row = $result->fetch_assoc()

    ) {


    $id = $row['id'];

    $name = $row['name'];

    $phone = $row['phone'];

    $phone2 = $row['phone2'];

    $email = $row['email'];

    $zipcode = $row['zipcode'];

    $address = $row['address'];

    $job = $row['job'];

    $description = $row['description'];

    $userid = $_SESSION['userid'];



    ?>

    <form action="update.php?id=<?php echo $id; ?>" method="post">

    <table cellpadding="10" cellspacing="0" width="500" class="tblSaveForm">

    <tr class="header">

    <td colspan="2">Edit Card</td>

    </tr>

    <tr>

    <td><label>Username</label></td>

    <td><input type="text" name="name" class="txtField" value="<?php echo $name; ?>"></td>

    </tr>

    <tr>

    <td><label>phone</label></td>

    <td><input type="text" name="phone" class="txtField" value="<?php echo $phone; ?>"></td>

    </tr>

    <td><label>phone2</label></td>

    <td><input type="text" name="phone2" class="txtField" value="<?php echo $phone2; ?>"></td>

    </tr>

    <tr>

    <td><label>email</label></td>

    <td><input type="text" name="email" class="txtField" value="<?php echo $email; ?>"></td>

    </tr>

    <tr>

    <td><label>zipcode</label></td>

    <td><input type="text" name="zipcode" class="txtField" value="<?php echo $zipcode; ?>"></td>

    </tr>

    <tr>

    <td><label>address</label></td>

    <td><input type="text" name="address" class="txtField" value="<?php echo $address; ?>"></td>

    </tr>

    <tr>

    <td><label>job</label></td>

    <td><input type="text" name="job" class="txtField" value="<?php echo $job; ?>"></td>

    </tr>

    <tr>

    <td><label>description</label></td>

    <td><input type="text" name="description" class="txtField" value="<?php echo $description; ?>"></td>

    </tr>

    <tr>

    <td colspan="2"><input type="submit" name="submit" value="Submit" class="buttom"></td>

    </tr>

    </table>

    </form>

} else {

    echo $mysqli->error;

}


查看完整回答
反对 回复 2022-09-30
  • 1 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信