猿问

使用预准备语句更新表

我正在尝试更新我的卡片表。用户选择“更新”按钮后,它会将他重定向到更新页面,在那里他可以看到和修改他的数据。问题是,输入字段不会加载他的数据,并且由于某种原因也无法更新它们。这是第一页上的按钮:


<a href="update.php?id=<?php echo $record['id']; ?>" class="btn btn-succes" role="button">Edit</a>

这是更新页面:


<?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());

}


$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>


达令说
浏览 101回答 1
1回答

HUX布斯

您缺少将用户输入放入语句中使用的所有变量的代码。并且更新应仅在提交表单时使用,而不是在您最初显示表单时使用。UPDATE您需要将 放入表单属性中的 URL 中,以便它知道要更新哪个 ID。idaction<?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){&nbsp; &nbsp; die("HIBA: Nem sikerült csatlakozni. " . mysqli_connect_error());}$id = $_GET['id'];if (isset($_POST['submit'])) {&nbsp; &nbsp; $name = $_POST['name'];&nbsp; &nbsp; $phone = $_POST['phone'];&nbsp; &nbsp; $phone2 = $_POST['phone2'];&nbsp; &nbsp; $email = $_POST['email'];&nbsp; &nbsp; $zipcode = $_POST['zipcode'];&nbsp; &nbsp; $address = $_POST['address'];&nbsp; &nbsp; $job = $_POST['job'];&nbsp; &nbsp; $description = $_POST['description'];&nbsp; &nbsp; $visibility = $_POST['visibility'];&nbsp; &nbsp; $confirmed = $_POST['confirmed'];&nbsp; &nbsp; $userid = $_POST['userid'];&nbsp; &nbsp; $stmt = $mysqli -> prepare('UPDATE cards SET name=?, phone=?, phone2=?, email=?, zipcode=?, address=?, job=?, description=?, visibility=?, confirmed=?, userid=?&nbsp; WHERE id = ?');&nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; $stmt &&&nbsp; &nbsp; &nbsp; &nbsp; $stmt->bind_param('ssssisssiiii', $name, $phone, $phone2, $email, $zipcode, $address, $job, $description, $visibility, $confirmed, $userid, $id) &&&nbsp; &nbsp; &nbsp; &nbsp; $stmt -> execute()&nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; echo 'Updated';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo $mysqli -> error;&nbsp; &nbsp; }}&nbsp;$getstmt = $mysql->prepare("SELECT * FROM cards WHERE id= ?");if ($getstmt and&nbsp; &nbsp; $getstmt->bind_param('i', $id) and&nbsp; &nbsp; $getstmt->execute() and&nbsp; &nbsp; $result = $getstmt->get_result() and&nbsp; &nbsp; $row = $result->fetch_assoc()&nbsp; &nbsp; ) {&nbsp; &nbsp; $id = $row['id'];&nbsp; &nbsp; $name = $row['name'];&nbsp; &nbsp; $phone = $row['phone'];&nbsp; &nbsp; $phone2 = $row['phone2'];&nbsp; &nbsp; $email = $row['email'];&nbsp; &nbsp; $zipcode = $row['zipcode'];&nbsp; &nbsp; $address = $row['address'];&nbsp; &nbsp; $job = $row['job'];&nbsp; &nbsp; $description = $row['description'];&nbsp; &nbsp; $userid = $_SESSION['userid'];&nbsp; &nbsp; ?>&nbsp; &nbsp; <form action="update.php?id=<?php echo $id; ?>" method="post">&nbsp; &nbsp; <table cellpadding="10" cellspacing="0" width="500" class="tblSaveForm">&nbsp; &nbsp; <tr class="header">&nbsp; &nbsp; <td colspan="2">Edit Card</td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>Username</label></td>&nbsp; &nbsp; <td><input type="text" name="name" class="txtField" value="<?php echo $name; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>phone</label></td>&nbsp; &nbsp; <td><input type="text" name="phone" class="txtField" value="<?php echo $phone; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <td><label>phone2</label></td>&nbsp; &nbsp; <td><input type="text" name="phone2" class="txtField" value="<?php echo $phone2; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>email</label></td>&nbsp; &nbsp; <td><input type="text" name="email" class="txtField" value="<?php echo $email; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>zipcode</label></td>&nbsp; &nbsp; <td><input type="text" name="zipcode" class="txtField" value="<?php echo $zipcode; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>address</label></td>&nbsp; &nbsp; <td><input type="text" name="address" class="txtField" value="<?php echo $address; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>job</label></td>&nbsp; &nbsp; <td><input type="text" name="job" class="txtField" value="<?php echo $job; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td><label>description</label></td>&nbsp; &nbsp; <td><input type="text" name="description" class="txtField" value="<?php echo $description; ?>"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td colspan="2"><input type="submit" name="submit" value="Submit" class="buttom"></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>&nbsp; &nbsp; </form>} else {&nbsp; &nbsp; echo $mysqli->error;}
随时随地看视频慕课网APP
我要回答