更新查询PHP - 擦除而不是更新

时间:2018-01-28 01:58:03

标签: php

这是针对作业的,因此代码基于学习资源的呈现方式。我有一个工厂数据库,我必须对其进行更改,然后更新plantID no.2。我创建了表单,然后填充了plantID 2信息,但是当我在进行更改后单击“更新”按钮时,它会擦除​​数据库中该条目的所有信息。我不确定我哪里出错了。任何帮助都会很棒。



<?php

// MySQL Database Connect
require_once("connect.php");
 
// read the values from the form and store in variables
$botanicName = $_POST['bot_name'];

$commonName = $_POST['comm_name'];

$plantDescription = $_POST['pl_desc'];

$commonUse = $_POST['comm_use'];

$maxHeight = $_POST['m_height'];

$maxWidth = $_POST['m_width'];

$popular = $_POST['pop'];

 
// escape variables for security

$botanicName = mysqli_real_escape_string($conn, $bot_name);

$commonName = mysqli_real_escape_string($conn, $comm_name);

$plantDescription = mysqli_real_escape_string($conn, $pl_desc);

$commonUse = mysqli_real_escape_string($conn, $comm_use);

$maxHeight = mysqli_real_escape_string($conn, $m_height);

$maxWidth = mysqli_real_escape_string($conn, $m_width);

$popular = mysqli_real_escape_string($conn, $pop);

 
// create the UPDATE query
$query="UPDATE plant SET botanicName='$botanicName', commonName='$commonName', plantDescription='$plantDescription', commonUse='$commonUse', maxHeight='$maxHeight', maxWidth='$maxWidth', popular='$popular' WHERE plantID='2'";
 
//execute the query

$results = mysqli_query($conn, $query );

// check for errors
if(!$results) {
    
echo ("Query error: " . mysqli_error($conn));
    
exit;
    
}

else {
    
// Redirect the browser window back to the make_changes page if there are no errors
    
header("location: ../make_changes.html");
}
?>
&#13;
<h2>Edit a Plant</h2>
    
<?php

// run a select query to return the existing data for the record
$query = "SELECT * FROM plant WHERE plantID='2'"; 
    
$results = mysqli_query($conn, $query );

// capture any errors    
if(!$results) { 
    
echo ("Query error: " . mysqli_error($conn));
    
}
    
else {
    
// fetch and store the results for later use if no errors
while ($row = mysqli_fetch_array($results)) {

$bot_name = $row['botanicName'];

$comm_name = $row['commonName'];

$pl_desc = $row['plantDescription'];

$comm_use = $row['commonUse'];

$m_height = $row['maxHeight'];

$m_width = $row['maxWidth'];

$pop = $row['popular'];
    
}
    
}
    
?>

<form method="post" action="code/update_plant.php">

<p>Botanic Name: <input type="text" name="botanicName" value="<?=$bot_name?>" required></p>

<p>Common Name: <input type="text" name="commonName" value="<?=$comm_name?>"required></p>

<p>Plant Description: <input type="text" name="plantDescription" value="<?=$pl_desc?>" required></p>

<p>Common Use: <input type="text" name="commonUse" value="<?=$m_height?>" required></p>

<p>Max. Height (m): <input type="text" name="maxHeight" value="<?=$m_height?>" required></p>

<p>Max. Width (m): <input type="text" name="maxWidth" value="<?=$m_width?>" required></p>

<p>Popular? (Y/N): <input type="text" name="popular" value="<?=$pop?>"required></p>

<input type="submit" name="submit" value= "Update">

</form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

发送到$_POST的参数在您的输入中包含name密钥,因此您的$_POST['bot_name']例如为空,获取该名称的正确方法为$_POST['botanicName']。< / p>

这将是您的帖子参数:

$botanicName = $_POST['botanicName'];

$commonName = $_POST['commonName'];

$plantDescription = $_POST['plantDescription'];

$commonUse = $_POST['commonUse'];

$maxHeight = $_POST['maxHeight'];

$maxWidth = $_POST['maxWidth'];

$popular = $_POST['popular'];

答案 1 :(得分:0)

您在表单中使用的名称必须与您在$ _POST中使用的索引完全匹配。您正在使用未定义的变量。

// read the values from the form and store in variables
$botanicName = $_POST['botanicName'];

$commonName = $_POST['commonName'];

$plantDescription = $_POST['plantDescription'];

$commonUse = $_POST['commonUse'];

$maxHeight = $_POST['maxHeight'];

$maxWidth = $_POST['maxWidth'];

$popular = $_POST['popular'];

修复mysqli转义函数调用:

// variable $bot_name does not exist therefore it results in a null value
$botanicName = mysqli_real_escape_string($conn, $bot_name); // bad
// Fixed
$botanicName = mysqli_real_escape_string($conn, $botanicName); // good

使表单输入名称与$ _POST

相同
<form method="post" action="code/update_plant.php">

<p>Botanic Name: <input type="text" name="botanicName" value="<?=$botanicName?>" required></p>

<p>Common Name: <input type="text" name="commonName" value="<?=$botanicName?>"required></p>

<p>Plant Description: <input type="text" name="plantDescription" value="<?=$plantDescription?>" required></p>

<p>Common Use: <input type="text" name="commonUse" value="<?=$maxHeight?>" required></p>

<p>Max. Height (m): <input type="text" name="maxHeight" value="<?=$m_height?>" required></p>

<p>Max. Width (m): <input type="text" name="maxWidth" value="<?=$maxWidth?>" required></p>

<p>Popular? (Y/N): <input type="text" name="popular" value="<?=$popular?>"required></p>

<input type="submit" name="submit" value= "Update">

</form>

答案 2 :(得分:0)

我需要更改$ _POST中的索引(我使用的是未定义的变量)并在mysqli转义函数中更改它们。