插入查询未插入数据库

时间:2013-10-21 10:01:45

标签: php mysql

我发现我的脚本没有任何问题,我不明白为什么它不会插入到我创建的数据库中。请仔细看看我的代码并告知可能出错的地方。 这是dbcon.php,它用于所有数据库连接

<?php
$host="localhost";
$user="root";
$password="";
$db="cottonboard";
$tablename="user";
mysql_connect($host,$user,$password) or die(mysql_error()); 
mysql_select_db($db) or die(mysql_error());
?> 

现在是将输入插入数据库的脚本,

<?php

include "dbcon.php";


extract($_POST);


/* just to test if all fields will be outputted
print_r($_POST);*/ 

$sql="INSERT INTO    $tablename(id,username,password,accesslevel,sex,contactnmuber,employer)
  VALUES('null','$usernameuser','$passworduser','$accesslevel','$gender','$contactnumberuser','$employeruser')" or die(mysql_error());

if($sql)
{
echo"we got here safe";
}

?>

2 个答案:

答案 0 :(得分:0)

// ...
$c = mysql_connect($host,$user,$password) or die(mysql_error()); 
// ...
mysql_query($sql, $c);

答案 1 :(得分:0)

$sql="INSERT INTO $tablename(id,username,password,accesslevel,sex,contactnmuber,employer) VALUES('null','$usernameuser','$passworduser','$accesslevel','$gender','$contactnumberuser','$employeruser')";

$mysqli = new mysqli($host, $user, $password, $db);

if ($mysqli->query($sql) === TRUE) {
   echo"we got here safe";
}

另请参阅:http://www.php.net/manual/en/mysqli.query.php