简体   繁体   English

如何使用受控语句和html表单通过php通过php向mysql db添加一行到用户输入?

[英]How do I add a row to mysql db via php using controlled statements and html form for user input?

I am trying to build an "admin" section of my website. 我正在尝试建立我网站的“管理员”部分。 One where I can update customer status on work orders (or tickets if you prefer the term). 我可以在其中更新工作订单(或机票,如果您喜欢的话)的客户状态的一种。 I have it where I can input an int in a text field and hit submit to DELETE , but I cannot get my addRow function to work. 我有它可以在文本字段中输入int并单击Submit to DELETE的地方 ,但是我无法使addRow函数正常工作。 It is not causing an error, which makes me believe that I am not passing my variables correctly. 它不会引起错误,这使我相信我没有正确传递变量。

Here are the forms on admin.php: 以下是admin.php上的表格:

<form name="newRow" METHOD="post" ACTION="q.php">
    Status of New Entry: <input type="text" value="Open" name="newStatus" /><br>
    Type of Maintenance being completed: <input type="text" value="Software Maintenance" name="maintType" /><br>
    <input type="submit" value="Add" name="newEntry" />
</form>

<form name="delRow" METHOD="post" ACTION="q.php">
    <input type="text" name="deleteID" />
    <input type="submit" value="Delete" name="delEntry"/>
</form>

As for my q.php, here is what I have after I connect to my db (which again, I have no problems using the delEntry/delRow section, so I can't see how a connection/mysqli initialization problem would be the issue: 至于我的q.php,这是我连接到数据库后的内容(再次,使用delEntry / delRow部分没有问题,所以我看不到连接/ mysqli初始化问题是怎么回事:

//prepare statements
$addData = $conn->prepare("INSERT INTO $tname (status, mainttype) VALUES (?, ?)");
$addData->bind_param("s,s", $newStatus, $maintType);

$delData = $conn->prepare("DELETE FROM $tname WHERE id=?");
$delData->bind_param("i", $deleteID);
//end prepared statements

//if New Entry Button is pressed
$newStatus = isset($_POST['newStatus'])
    ? $_POST['newStatus']
    : '';
$maintType = isset($_POST['maintType'])
    ? $_POST['maintType']
    : '';
$addData->execute();
if ( false===$addData ) {
    die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
else{
    printf("rows inserted: %d\n", $addData->affected_rows);
}

//if Del Entry Button is pressed
if ( isset( $_POST['delEntry'] ) ) {
    $deleteID = $_POST['deleteID'];
    $delData->execute();
}

$addData->close();
$delData->close();
$conn->close();
?>

my columns are matching according to phpMyAdmin: 我的列根据phpMyAdmin进行匹配:

$addData = $conn->prepare("INSERT INTO $tname (status, mainttype) VALUES (?, ?)");

status and mainttype (yes 2 t). 状态和维护类型(是2吨)。 my ID (primary) is an auto_incriment so I left it out because I don't want to cause any key duplicate errors by accident. 我的ID(主要)是auto_incriment,因此我将其省略了,因为我不想偶然导致任何重复的密钥错误。 It's auto_incriment has been tested and seems to be working fine. 它的auto_incriment已经过测试,似乎工作正常。

Too make it more fun, I added an echo $newStatus; 太有趣了,我添加了echo $ newStatus; after my prepared statement execution, and it comes back with the correct value. 在我准备好的语句执行后,它会返回正确的值。 I appear to be having a problem with the addition of the new row. 我似乎在添加新行时遇到问题。 Still no error being generated. 仍然没有错误生成。

printf("rows inserted: %d\n", $addData->affected_rows);

returns with 0 rows affected as well. 也会返回0行。

Simple comma issue. 简单的逗号问题。 On: 上:

$addData->bind_param("ss", $newStatus, $maintType);

I had it listed as: 我把它列为:

$addData->bind_param("s,s", $newStatus, $maintType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM