php使用insert语句添加到数据库

时间:2014-01-17 14:29:48

标签: php mysql database insert

我有一个简单的insert语句,它应该使用带有我的连接字符串的$ GLOBALS。问题是它没有插入数据。我这样做了吗?

Insert.php

<?php
require 'core/init.php';

$Name = $_REQUEST["Name"];
$Venue = $_REQUEST["Venue"];
$Category = $_REQUEST["Category"];

$query = "INSERT INTO bands (Name, Venue, Category)
          VALUES ('$Name', '$Venue', '$Category')";
mysql_query ($query,$GLOBALS)
    or die ("could not add to database");

echo("You have added: <br />");
$result = mysql_query("SELECT * FROM bands ORDER  BY  Band_id DESC LIMIT 1");

while($row = mysql_fetch_array($result))
{
    echo $row['Name']. "" . $row['Venue']. "" . $row['Category'];
    echo "<br />";
}
?>

2 个答案:

答案 0 :(得分:1)

你需要用mysql_connect()函数连接mysql然后用mysql_select_db()函数选择数据库然后你可以调用mysql_query函数

你不能用查询字符串参数执行查询,你需要首先执行该连接查询,然后选择你的数据库

And i suggest you to use mysqli instead of mysql cuz mysql methods will be deprecated soon

答案 1 :(得分:0)

您不会在$GLOBALS来电中添加整个mysql_query()变量。您只使用连接字符串放置特定变量:

mysql_query($query, $GLOBALS['connection']) // Assuming you called the connection var "connection"
    or die ("could not add to database");