你好 Pas-6ren">
gpt4 book ai didi

PHP输入mysql数据库

转载 作者:行者123 更新时间:2023-11-30 23:22:49 25 4
gpt4 key购买 nike

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

<?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>

你能帮我弄到它,这样当他们把它放进数据库时吗?我想要它,但它不是,所以请帮忙。

最佳答案


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

<?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();
?>

关于PHP输入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com