gpt4 book ai didi

PHP插入工作正常但数据进不去

转载 作者:行者123 更新时间:2023-11-29 08:02:04 24 4
gpt4 key购买 nike

这是我的代码,如果我检查 PHP 错误日志,没有错误,据我所知,数据库已连接并且数据正在处理,但它没有显示在数据库表“日历”中?

这是 config.php:

<?php

global $db;
global $productid;

$db = mysqli_connect("localhost", "********", "********", "database");

if (mysqli_connect_errno($db)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// checks if user is logged in for admin panel
function isLoggedin ($db)
{
if (isset($_SESSION['login'])) {

$query = mysqli_query($db, "SELECT ID, username FROM sr_users WHERE ID='".mysqli_real_escape_string($db, $_SESSION['login_userId'])."'") or die(mysql_error());
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);

if ($_SESSION['login_hash'] == hash('sha512', $row['username'] . $row['ID'])) {
return true;
} else {
session_destroy();
header("location: login.html");
}
} else {
session_destroy();
header("location: login.html");
}
}

if (isset($_GET['productid'])) {
if (!empty($_GET['productid'])) {
$productid = $_GET['productid'];
} else {
$productid = '';
}
} else {
$productid = '';
}
?>

这里是日期选择器(使用 jquery-ui)将日期插入日历数据库:

<?php 

session_start();

include_once("./config.php");

isLoggedin ($db);

?>

<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({dateFormat: 'yy-mm-dd' });
});
</script>
</head>
<body>

<?php

$form = "

<form action='' method='post' id='RegisterForm' autocomplete='off'>

<table>

<tr>

<td><p>Date: <input type='text' name='datepicker' id='datepicker'></p></td>

</tr>

<tr>

<td><input type='submit' name='submitbtn' value='Register'></td>

</tr>

</table>

</form>


";

if ($_POST['submitbtn'])

{

$datepicker = strip_tags($_POST['datepicker']);

if ($datepicker)
{

mysqli_query($db, "INSERT INTO calendar (date)
VALUES ('$datepicker')");

echo "<h2>Done!!!</h2>";

}
else

echo "Please enter a valid date";

}

else

echo "$form";

?>


</body>
</html>

我正在使用我的星级评论 ( http://codecanyon.net/item/starreviews-ajax-jquery-rating-and-review-form/6584956 ) 插件中的数据库用户,并且我为事件日历 ( http://www.phpjabbers.com/free-availability-calendar-script/ ) 添加了一个名为“日历”的额外表,我需要将数据插入到?有什么想法吗?

最佳答案

代码应该是:

    if ($datepicker) 
{

mysqli_query($db, "INSERT INTO calendar (date)
VALUES ('" . mysqli_real_escape_string($db, $datepicker) . "')") or die(mysqli_error($db));

您遗漏了指定要使用的数据库连接的参数。这应该会生成类似 mysqli_query Expectsparameter 1 to be mysqli 的错误。

我添加了数据转义,以及插入失败时的错误报告。

关于PHP插入工作正常但数据进不去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553975/

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