MYSQLi PHP连接

时间:2016-05-07 05:30:56

标签: mysqli

以下是一个有效的mysqli php connect insert语句吗?

if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))
{
?>
    <script>alert('successfully registered ');</script>
    <?php
}
 else
{
?>
    <script>alert('error while registering you...');</script>
    <?php
}
}

但是我在收到错误时收到错误“注册你......”

由于

3 个答案:

答案 0 :(得分:0)

问题已解决。 &#34; user_id&#34;存在问题。正在更新的列主键重复(0值)此错误&#34;重复条目&#39; 0&#39;关键&#39; PRIMARY&#39;。在phpmyadmin中使用自动增量更新user_id列后,INSERT查询已成功更新。

使用以下代码检查错误有帮助。

 die(mysqli_error($conn));

答案 1 :(得分:-1)

一个简单的例子:

<?php  
//Create the connection  

$con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));  

// Write query

$strSQL = "SELECT username FROM MyTable";

// Execute the query.

$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
  echo $result["username"]."
";
}

// Close the connection
mysqli_close($con);

?>

答案 2 :(得分:-1)

你能用连接语句粘贴整个块吗? 否则SQL语句看起来很好。你不应该将输入转义为MySQL

if($mysql_query = $conn->query("INSERT INTO users (username,password,question,securityanswer) VALUES ('$uname','$upass','$selectquestion','$answer')"))

Why using if($mysql_query = $conn->query ?

更好

if($ conn-&gt; query(&#34; INSERT INTO users(username,password,question,securityanswer)VALUES(&#39; $ uname&#39;,&#39; $ upass&#39;, &#39; $ selectquestion&#39;,&#39; $ answer&#39;)&#34;)=== TRUE)