简体   繁体   中英

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. I'm using xmlHttpRequest.open() to pass the values to this page the call statement is:

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()函数的输出上起作用,即,您要提供字符串的对象期望对象的功能

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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