gpt4 book ai didi

php - 向数据库发送 0 个值

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

我创建了第一个将数据发送到数据库的 ajax 脚本。但是有一个问题,新创建的行中有 0 个值。在 Firebug 中,我可以看到 php 文件中有这些值。

Parametersapplication/x-www-form-urlencodedDo not sort
currentTasken
1415
currentUser
37
Source
currentUser=37&currentTasken=1415

但是他们无法访问数据库,这是我的 php 文件。

<?php
$currentUser = isset($_POST['$currentUser']);
$currentTasken = isset($_POST['$currentTasken']);
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows >= 1)
{
//$pass = md5($pass);

$ins = mysql_query("INSERT INTO dotp_task_log (task_log_creator, task_log_Task) VALUES ('$currentUser' , '$currentTasken')" ) ;

if($ins)
die("Succesfully Created Log!");

else
die("ERROR");

}
else
{
die("Log already exists!");
}

?>

这是数据库

5   0   NULL    NULL    0   0   NULL        0   0   NULL

最佳答案

您的代码中有 2 个错误。

$currentUser = isset($_POST['$currentUser']);

1:我相信你错了,你的意思是:

$currentUser = isset($_POST['currentUser']);

2: isset 是一个函数,它返回一个 bool 值,以判断变量是否被定义和设置。所以 $currentUser 变量现在保存 bool 值(0 或 1)而不是发布数据的值。

你应该这样使用它:

$currentUser = isset($_POST['currentUser']) ? $_POST['currentUser'] : '';

这是 if-else 条件的简短版本。如果设置了 $_POST['currentUser'],变量 $currentUser 应该保持它的值,否则 - 它会有一个空字符串。

补充说明:

  • 请记住,您应该清理 $_POST 数据。
  • 您正在使用已弃用的 mysql_*。考虑改用 mysqliPDO

关于php - 向数据库发送 0 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024148/

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