这是在提交时运行 web.php 的 Java 脚本 $(document).ready(function() { $('form')-6ren">
gpt4 book ai didi

php - 如果文本之间有单引号,我的 php 表单提交到 mysql 不起作用

转载 作者:行者123 更新时间:2023-11-29 00:22:16 24 4
gpt4 key购买 nike

这是我的 html 页面:

<form method="post">
<input type="hidden" name="user" readonly="readonly" value="<?php echo $session->username;?>">
<input placeholder="TAS Code" class="input-text" type="text" name="customerCode" id="name" />
<textarea type="text" placeholder="Comments" style="width: 500px;" wrap="hard" class="input-text" name="comments" id="email"/></textarea>
<input type="submit" value="Submit" class="btn-u btn-u-small" name="submit" id="comment-submit" />
</form>

这是在提交时运行 web.php 的 Java 脚本

$(document).ready(function() {
$('form').submit(function(msg) {
$.post("web.php",$(this).serialize(),function(data){
});
$( 'form' ).each(function(){
this.reset();
});
return false;

最后是我的 web.php 来处理表单内容。

$mysqli =mysqli_connect("localhost","pf_sales","pfsal3s","pf_sales");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$webbox = $_POST['web'];
$codebox=$_POST['customerCode'];
$commentbox=$_POST['comments'];
$discount=$_POST['discount'];
$task=$_POST['taskbox'];
$clear=$_POST['clearbox'];
$user=$_POST['user'];


if ($codebox !="" && $commentbox !="" && $webbox!="" && $discount!="")
{
$query = "INSERT INTO commentstable (customerCode, comments) VALUES ('$_POST[customerCode]', '$_POST[comments]');";
$query .= "UPDATE webdiscount SET web = ('$_POST[web]'),discount=('$_POST[discount]') where customerCode = ('$_POST[customerCode]');";

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
}
if ($codebox !="" && $commentbox !="" && $webbox!="")
{
$query = "INSERT INTO commentstable (customerCode, comments) VALUES ('$_POST[customerCode]', '$_POST[comments]');";
$query .= "UPDATE webdiscount SET web = ('$_POST[web]') where customerCode = ('$_POST[customerCode]');";

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
}
if ($codebox !="" && $commentbox !="" && $task!="")
{
$query = "INSERT INTO commentstable (customerCode, comments) VALUES ('$_POST[customerCode]', '$_POST[comments]');";
$query .= "UPDATE webdiscount SET taskFor=('$_POST[taskbox]') where customerCode = ('$_POST[customerCode]');";

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
}
else if ($codebox!="" && $commentbox!="" && $user!="")
{
$query ="INSERT INTO commentstable (user, customerCode, comments) VALUES ('$_POST[user]', '$_POST[customerCode]', '$_POST[comments]');";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());

}
$mysqli->close();
}

else if ($codebox!="" && $webbox!="" )
{
$query ="UPDATE webdiscount SET web = ('$_POST[web]') where customerCode = ('$_POST[customerCode]');";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());

}
$mysqli->close();
}
else if ($codebox!="" && $discount!="" )
{
$query ="UPDATE webdiscount SET discount=('$_POST[discount]') where customerCode = ('$_POST[customerCode]'); ";
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());

}
$mysqli->close();
}
else if ($codebox !="" && $task!="")
{

$query = "UPDATE webdiscount SET taskFor=('$_POST[taskbox]') where customerCode = ('$_POST[customerCode]');";

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
}
else if ($codebox !="" && $clear=="done")
{

$query = "UPDATE webdiscount SET taskFor='' where customerCode = ('$_POST[customerCode]');";

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();

所以当我们放置 TAScode 和评论时,它应该更新评论表。只要我们不在文本中使用单引号 ',它就可以正常工作。即,如果我们写“我们没有这样做”,那么表单不会更新数据库。

任何帮助将不胜感激。谢谢

最佳答案

问题是因为您将未转义的单引号传递给您的 SQL 查询。例如,如果 $_POST['customerCode'] 包含 We didn't do this 您的查询将如下所示:

INSERT INTO commentstable (customerCode, comments) VALUES ('We didn't do this', '…')

您看到破坏 SQL 语句的额外引号了吗?您可以通过使用 mysqli::real_escape_string 或更好的方法,通过切换到准备好的语句来防止这种情况。您的查询将如下所示:

$query = "INSERT INTO commentstable (customerCode, comments) VALUES (?, ?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('is', $_POST['customerCode'], $_POST['comments']);
$stmt->execute();

顺便说一句,您真的应该使用 issetempty 检查这些 POST 变量是否存在,否则如果未通过,您将收到错误消息。

关于php - 如果文本之间有单引号,我的 php 表单提交到 mysql 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20657268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com