php - SQL 命令正在执行,但在 mysqli 连接中返回 false

标签 php mysql

我有一个使用 html 代码在 php 中创建的网页。我想将在我的网页中输入的用户信息保存到 MySQL 数据库中。我使用 php 作为中间人将前端网页(htmnl 代码)链接到数据库(mysql)。

在我的链接文件夹(中间人 php 文件)中,我有以下内容:

<?php
//Gets server connection credentials stored in serConCred2.php
require_once('ConCred2.php');

//SQL code for connection w/ error control
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(!$con){
die('Could not connect: ' . mysqli_connect_error());
}
//Selection of the databse w/ error control
$db_selected = mysqli_select_db($con, DB_NAME);

if(!$db_selected){
die('Can not use ' . DB_NAME . ': ' . mysqli_error($con));
}


//Co-PI and Co-Investigator Information variables
$Co_FNAME = $_POST['fname'];
$Co_LNAME = $_POST['lname'];
$Co_SLNAME = $_POST['slname'];
$Co_DEGREE = $_POST['Degree_Selection'];
$Co_DEGREE_Other = $_POST['other_specify_degree']; //hold the value of degree if user selected other from the dropdown menu
$Co_CPOS = $_POST['Current_Position_Selection'];
$Co_CPOS_Other = $_POST['other_specify_cpos']; //hold the value of Current Position if user selected other from the dropdown menu
$Co_INST = $_POST['Institution_Selection'];
$Co_INST_Other = $_POST['other_specify_inst']; //hold the value of Current Position if user selected other from the dropdown menu
$Co_SCHOOL = $_POST['School_Selection'];
$Co_SCHOOL_Other = $_POST['other_specify_school']; //hold the value of Current Position if user selected other from the dropdown menu
$Co_DEPART = $_POST['Department_Selection']; //Este se estara eliminando en la version online
$Co_DEPART_Other = $_POST['other_specify_department']; //hold the value of Department if user selected other from the dropdown menu
$Co_PROGRAM = $_POST['program'];
$Co_EMAIL = $_POST['email'];
$Co_PHONE = $_POST['phone'];


//If decition when user select other from the dropdown menu
if($Co_DEGREE == "other_degree") $Co_DEGREE = $Co_DEGREE_Other;
if($Co_CPOS == "other_cpos") $Co_CPOS = $Co_CPOS_Other;
if($Co_INST == "other_inst") $Co_INST = $Co_INST_Other;
if($Co_SCHOOL == "other_school") $Co_SCHOOL = $Co_SCHOOL_Other;
if($Co_DEPART_Other == "other_department") $Co_DEPART = $Co_DEPART_Other;

//This sets a starting point in the rollback process in case of errors along the code
$success = true; //Flag to determine success of transaction
//start transaction
echo "<br>1. Going to set autocommit to 0";
$command = "SET AUTOCOMMIT = 0";
echo "<br>2. Autocomint has been set to 0";

echo "<br>3. Going to run query to see if result is true or false";
$result = mysqli_query($con, $command);
echo "<br>4. Finished running the query. Result is:" . $result;

echo "<br>5. Going to set command to BEGIN";
$command = "BEGIN";
echo "<br>6. Command is now BEGIN";

echo "<br>7. Going to run query for command BEGIN";
$result = mysqli_query($con, $command);
echo "<br>8. Query runned for command BEGIN";

echo "<br>9. Result value is: " . $result;
//Saves Pi values into database
/**
$sqlCoPI = "INSERT INTO co_pi_table (Fname, Lname, SLname, Degree, Current_Position, Institution, School, Department, Program, Email, Phone)
                        VALUES('$Co_FNAME', '$Co_LNAME', '$Co_SLNAME', '$Co_DEGREE', '$Co_CPOS', '$Co_INST', '$Co_SCHOOL', '$Co_DEPART', '$Co_PROGRAM', '$Co_EMAIL', '$Co_PHONE')";
*/


echo "<br>10. Going to write sql command to populate table pi_table";
/**
$sqlPi = "INSERT INTO pi_table (Fname, Lname, SLname, Degree, Current_Position, Institution, School, Department, Program, Email, Phone)
                        VALUES('$Co_FNAME', '$Co_LNAME', '$Co_SLNAME', '$Co_DEGREE', '$Co_CPOS', '$Co_INST', '$Co_SCHOOL', '$Co_DEPART', '$Co_PROGRAM', '$Co_EMAIL', '$Co_PHONE')";                 
*/

$sqlPi = "INSERT INTO pi_table (Fname) VALUES('$Co_FNAME')";                    


                        //Checks to see if theres an error in the pi db con
echo "<br>11. Sql command finished writting.";  

echo "<br>12. Going to query the sql finished command to the database to determine value of result.";
    $result = mysqli_query($con, $sqlPi);
echo "<br>13. Finished running sql command to database. Result value is: " . $result;   

echo "<br>14. Going to enter if statements depending on result value";
    if($result == false){
    //die ('<br>Error in query to PI table: ' . mysqli_error($con));
    echo "<br>15. I am inside the false statement. Success is going to be set as false. ";
    $success = false;
    //$success = true; //Cahnged this in order to test if values are being saved to db. Change back to false.
}

 //Checks for errors or craches inside the code
 // If found, execute rollback

 echo "<br>16. Going to verify is success is true.";
 if($success){
     $command = "COMMIT";
     $result = mysqli_query($con, $command);
     //echo "<br>Tables have been saved with 0 errors.";
     echo "<br><p style=\"color: red;\"Principal Investigator has been saved successfuly. <br><br>
     You may now CLOSE this page and press the<br><br> \"Refresh List\" <br><br>
     button to display name in dropdown menu selection.</p>";
 }
 else{
     $command = "ROLLBACK";
     $result = mysqli_query($con, $command);

     echo "<br>17. Success was determined to be false.";
     echo "<br>Error! Databases could not be saved.<br>
     Contact system manager to report error. <br> <br>" . mysqli_error($con);
 }

 echo "<br>18. Setting autocommit back to 1 again.";
 $command = "SET AUTOCOMMIT = 1"; //return to autocommit
 $result = mysqli_query($con, $command);


//Displays message 
//echo '<br>Connection Successfully. ';
//echo '<br>Database have been saved';

//Close the sql connection to dababase
mysqli_close($con)                      

?>

如您所见,我要求用户填写他们的信息。一些所需的信息是下拉菜单字段,用户可以从显示的选项中选择一个选项。

我遇到的问题是,当上面的 php 代码执行时,它确定 $result 变量为 false 并且不保存任何内容。执行代码时,会显示以下消息:

1. Going to set autocommit to 0
2. Autocomint has been set to 0
3. Going to run query to see if result is true or false
4. Finished running the query. Result is:1
5. Going to set command to BEGIN
6. Command is now BEGIN
7. Going to run query for command BEGIN
8. Query runned for command BEGIN
9. Result value is: 1
10. Going to write sql command to populate table pi_table
11. Sql command finished writting.
12. Going to query the sql finished command to the database to determine value of result.
13. Finished running sql command to database. Result value is: 
14. Going to enter if statements depending on result value
15. I am inside the false statement. Success is going to be set as false. 
16. Going to verify is success is true.
17. Success was determined to be false.
Error! Databases could not be saved.
 Contact system manager to report error. 


18. Setting autocommit back to 1 again.

出于安全考虑,我不能发布 html 内容,因为它包含敏感的名称信息和数据库。尽管我可以确保数据库中的表完全按照 sql 命令行中的方式调用。

最佳答案

我找到了问题!

经过长时间的辩论,我决定重新创建存储所有信息的数据库。当我在我的 sql 命令中重定向表时(我没有将它保存在“pi_table”中,而是将它保存在一个名为“pi_table_2”的新创建的数据库中)并且一切正常。

显然我的数据库损坏了,phpMyAdmin 没有意识到它被篡改了。

用于引用我的 InnoDB 格式的数据库表。是什么导致了这种情况的发生,谁知道呢,但是如果您遇到过类似的问题,请创建一个小型测试数据库并查看它是否可以保存。如果是,请重新创建表格,它可能会像解决我的问题一样解决您的问题。

再次感谢大家!!!!

关于php - SQL 命令正在执行,但在 mysqli 连接中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974893/

相关文章:

php - Laravel - php artisan config :cache - [ReflectionException] Class cache. 商店不存在

php - 是否有替代事务允许对表执行多个并发查询并确保表处于一致状态?

php - 在 MySQL 中获取 start_date 和 end_date 内的项目

php - 对于高度变化的网站,memcache 或 memcached 有什么好处?

php - 使用 php echo mysql 查询的几行和几列

php - 保存div内容到数据库

php - MySQL - 在 PHP 中批量重命名所有行

mysql - 如何使用 Knex 将函数应用于 where 子句的值?

mysql - 在 MYSQL 中使用 DISTINCT 和各种其他条件时查询速度慢(1000 万条记录)

php - MySQL - 将行显示为列(尽可能简单)