-2
<html>
   <body>
   <?php
    $host = 'localhost';
    $user = 'users';
    $pw = '';
    $db = '#######';
    $connect = mysql_connect($host,$user,$pw)
               or die ("Could not connect.");

    mysql_select_db($db);

    $sql = mysql_query("INSERT INTO users VALUES ('','l','l','l','l','l','l')");
    if(mysql_query($sql)){
       print "Item added successfully.<br/>";
    }
    else{
       print "Item addition failed.<br/>";
    }
 ?>
</body>
</html>

我正在尝试将这些值插入我的数据库到表中users,但是当我运行代码时,它一直说“项目添加失败”,但我不知道为什么,数据库连接没有问题,因为我已经测试过了,而且我不确定我的插入查询有什么问题?

4

4 回答 4

3

There are some problems in your code. Firstly you do query twice. Secondly are you sure your db name is $db = '#######'; change it to proper name.

To check if query was ok and if rows were added use mysql_affected_rows() to check errors use mysql_error()

Also change your sql engine to PDO or mysqli which are better.

Please mind that Mysql_* functions are depracated. That is why I've given you example how to use db connection in PDO.

<?php
   $host = 'localhost';
   $user = 'users';
   $pw = '';
   $db = '#######'; //CHANGE IT TO PROPER NAME WHERE TABLE users IS!
   $connect = mysql_connect($host,$user,$pw)
       or die ("Could not connect.");

   mysql_select_db($db);

  $sql = mysql_query( "INSERT INTO users VALUES('' ,'l','l','l','l', 'l','l')") or die(mysql_error());

  if(mysql_affected_rows()>0)
     echo "Item added successfully.<br/>";
  else
     echo "Item addition failed.<br/>"; 
?>

I'll give you proper example how to do it with PDO cause if you still learn it'll help you :)

PDO example

<?php
$dsn = 'mysql:dbname=YOUR_DB_NAME;host=localhost';
$user = 'users';
$password = '';

try {
    $dbh = new PDO($dsn, $user, $password);
    $count = $dbh->exec("INSERT INTO users VALUES('' ,'l','l','l','l', 'l','l');");
    echo $cout ? "Item added successfully" : "Item addition failed";
} catch (PDOException $e) {
    echo 'Failed: ' . $e->getMessage();
}

?>

The secure and good way to insert values is using prepared statements. To create prepared statement you use

$stmt = $dbh->prepare("INSERT INTO users (name, email) VALUES(?,?)");
$stmt->execute( array('user', 'user@example.com')); 

You can learn more here

于 2013-05-09T10:50:01.947 回答
1

试试 this.your 调用的函数 mysql_query() 两次。第二次您将结果集而不是查询传递给它。

     <html>
     <body>
      <?php
       $host = 'localhost';
       $user = 'users';
       $pw = '';
       $db = '#######';
       $connect = mysql_connect($host,$user,$pw)
           or die ("Could not connect.");


       mysql_select_db($db);

      $sql = mysql_query( "INSERT INTO users 
       VALUES('' ,'l','l','l','l', 'l','l')");

     if($sql){
        print "Item added successfully.<br/>";
      }
     else { print "Item addition failed.<br/>"; }
    ?>
    </body>
     </html>
于 2013-05-09T10:40:09.093 回答
1

这应该可以帮助您调试,但是您应该查看PDO ...当然要记住使用正确的凭据,我认为您已将其删除以确保安全:-)

mysql_select_db($db);

$sql = mysql_query("INSERT INTO users VALUES ('','l','l','l','l','l','l')", $connect) OR die(mysql_error());
if($sql)
{
   print "Item added successfully.<br/>";
}
else{
   print "Item addition failed.<br/>";
}
?>
于 2013-05-09T10:53:20.553 回答
1

您正在使用mysql_query两次。将您的代码更改为:

 $sql = "INSERT INTO users VALUES ('' ,'l','l','l','l', 'l','l')";
于 2013-05-09T10:34:42.610 回答