【问题标题】:Why does the following not appear to open an SQL connection?为什么以下似乎没有打开 SQL 连接?
【发布时间】:2017-10-16 21:54:38
【问题描述】:

我发现以下脚本由于某种原因挂起。它将加载并且 PHP 看不到任何错误,但它不会处理数据(请注意,我们处于打开单独登录数据库的上下文中。)

在 process.php 中我们有以下内容:

<? PHP
//Process the POST data in prepration to write to SQL database.
$_POST['chat_input'] = $input;   
$time = date("Y-m-d H:i:s");    
$ip = $_SERVER['REMOTE_ADDR'];   
$name = $_SESSION['username'];    
$servername = "localhost";
$username = "id3263427_chat_user";
$password = "Itudmenif1!Itudmenif1!";
$dbname = "id3263427_chat_user";
$id = "NULL";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

if($link === false){
   die("ERROR: Could not connect. " .  mysqli_connect_error());
}

$sql = 'INSERT INTO `chat` (`id`, `username`, `ip`, `timestamp`,   
        `message`)  VALUES ('$id','$name', '$ip', '$time', '$input')';

if(mysqli_query($link, $sql)){ 
    mysqli_close($conn);
    header('Location: ../protected_page.php');
} else { 
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

?>

上面传递给脚本的html表单如下:

<form action="/process.php" method="post" id="chat">
    <b> Send A Message (500 Character Max):</b><br>
    <textarea name="chat_input" form="chat" size="500"></textarea>
    <input type="submit" value=submit>
</form> 

不知道这是怎么回事。

【问题讨论】:

  • 您确定要执行此$_POST['chat_input'] = $input; 而不是此$input = $_POST['chat_input'];
  • 我纠正了这一点,无论发生什么似乎都没有影响。
  • 你确定没有报错检查你的sql字符串,如果你对列名进行简单引用,它通常必须用双引号括起来
  • 您还为连接使用了两个不同的变量名称,$conn$link。尝试更改为$link = mysqli_connect($servername, $username, $password, $dbname);
  • 检查 php.ini 中的 display_errors 设置。这在默认情况下经常关闭,但应该在开发环境中打开。

标签: php windows apache http web-deployment


【解决方案1】:

您收到语法错误,因为您使用 ' 关闭 $id 之前的 $sql 字符串。

你的 $id 变量是怎么回事?使用您当前的代码,您将插入字符串“NULL”。如果你想将 sql 值设置为 null,你应该使用 $id = null; 或者不插入任何值。

如果您希望您的数据库设置一个 id,也请将其留空。

$input = $_POST['chat_input'];
$id = null;

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
  die("ERROR: Could not connect. " .  $conn->connect_error);
}

第一个解决方案

如果这不是生产代码,您可以将变量直接插入到语句中,但您应该使用" 而不是' 作为您的sql 字符串,这样您就可以插入变量和' 而无需关闭字符串。

$sql = "INSERT INTO chat (id, username, ip, timestamp, message) VALUES ('$id', '$name', '$ip', '$time', '$input')";
if($conn->query($sql) === true) {
 $conn->close();
 header('Location: ../protected_page.php');
} else {
 echo "ERROR: Could not able to execute $sql. " .$conn->error;
 $conn->close();
}

第二种解决方案

更好的方法是准备好声明。

$stmt = $conn->prepare('INSERT INTO chat (username, ip, timestamp, message) VALUES (?, ?, ?, ?)');
$stmt->bind_param("ssss", $username, $ip, $time, $input);

if($stmt->execute()) {
 $stmt->close();
 $conn->close();
 header('Location: ../protected_page.php');
} else {
 echo "ERROR: Could not able to execute $stmt. " . $conn->error;
 $stmt->close();
 $conn->close();
}

bind_param() 中的"s" 在给定位置定义了一个字符串,如果要插入整数,请改用"i"

例如bindParam("sis", $string, $integer, $string);

【讨论】:

    猜你喜欢
    • 2015-01-14
    • 2011-03-12
    • 2012-01-24
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2021-10-23
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多