简体   繁体   English

我无法使用PHP插入MySQL(无错误显示)

[英]I'm unable to insert into MySQL using PHP (no errors are being displayed)

<?php
$q= $_REQUEST["q"];
$r = $_REQUEST["r"];
$s = $_SESSION['empid'];
$max = 0;
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employeesurvey';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$sql1 = "SELECT QuestionID FROM question";
if(!mysqli_query($conn,$sql1)){
    echo 'error2 php';
}
while($rw1 = mysqli_fetch_array($sql1)){
   $Q = $rw1['QuestionID'] ;
   if ($max<$Q){
       $max = $Q;
   }
}
$Q = $Q+1;
$sql = "INSERT INTO question VALUES (".$Q.",'".$r."',".$s.",CURRENT_DATE(),".$q.",0)";
if(!mysqli_query($conn,$sql)){
   echo "Error";
}

?> ?>

The db, table names are all correct. db,表名都正确。 I'm using xmlHttpRequest.open() to pass the values to this page the call statement is: 我正在使用xmlHttpRequest.open()将值传递给此页面,调用语句为:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "gethint1.php?q=" + cid + "&r=" + question, true);

Im not getting any errors, nor the values are being inserted 我没有收到任何错误,也没有插入值

Replace this line: 替换此行:

if(!mysqli_query($conn,$sql1)){

with these 用这些

$resultSet = mysqli_query($conn,$sql1);
if(!$resultSet){

And now replace this line: 现在替换此行:

while($rw1 = mysqli_fetch_array($sql1)){

With this one 有了这个

while($rw1 = mysqli_fetch_array($resultSet)){

Reason is that you haven't executed query and stored the result set while at fetching record from result set, you are using direct query variable which is logically wrong. 原因是在从结果集中获取记录时,您没有执行查询并存储结果集,而是使用直接查询变量,这在逻辑上是错误的。

为什么通过从表中获取问题ID来使一件简单的事情变得如此复杂,只需在mysql表中使用autoincrement字段或使用insert_id,问题是mysqli_fetch_array()函数在mysqli_query()函数的输出上起作用,即,您要提供字符串的对象期望对象的功能

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

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