1

我正在尝试将输入表单放入我的数据库中,但它不起作用。这是我的代码:

<?php

$host="localhost"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="input"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$email=$_POST['email'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(email) VALUES('$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>

你好

<form class="form-signin" method="post" action="./php/email2db.php">
<input type="text" name="email" class="input-block-level" placeholder="Email address">
<button class="btn btn-primary" type="submit">Pass The Popcorn</button>
</form>

你能帮我得到它,所以当他们把它拿出来时,它会进入数据库吗?我想要它,但不是这样,请帮忙。

4

3 回答 3

0


我认为您的查询应如下所示:

<?php

$host="localhost"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="input"; // Table name 

// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name)or die("cannot select DB");
//Removed the " from $db_name, $host, $username, $password

// Get values from form 
$email=$_POST['email'];

// Insert data into mysql 
$sql="INSERT INTO ".$tbl_name." (email) VALUES(".$email.")";
//Here you have to concatenate the query from multiple parts, this is important
//And you cant use "$tbl_name(email)"
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php 
// close connection 
mysql_close();
?>
于 2013-06-14T18:05:32.973 回答
0

你的代码应该是

<?php
$host="localhost"; // Host name 

$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="input"; // Table name 

// Connect to server and select database.
$conn   =   mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name,$conn)or die("cannot select DB");
//Removed the " from $db_name, $host, $username, $password
// Get values from form 
$email=$_POST['email'];
// Insert data into mysql 
$sql="INSERT INTO `".$tbl_name."` (`email`) VALUES('".$email."')";
//Here you have to concatenate the query from multiple parts, this is important
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". 
if($result)
{
    echo "Successful";
    echo "<br/>";
    echo "<a href='insert.php'>Back to main page</a>";
}

else 
{
    echo "ERROR";
}
?> 
于 2013-06-14T18:12:47.520 回答
0

通过使用INSERT INTO $tbl_name(email) VALUES('$email'),您基本上是在说您的表的名称是 $tbl_name(email) 原样,但事实并非如此。您需要将 sql 重写为 INSERT INTO ".$tbl_name"。(电子邮件) VALUES(".$email.")

于 2013-06-14T18:15:37.193 回答