gpt4 book ai didi

php - 连接两个 HTML 输入并将它们插入 MySQL 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:50 25 4
gpt4 key购买 nike

我正在为我所在的这个类(class)创建这个网页,为此我需要连接两个单独的 HTML 表单输入,中间有一个空格,并将它们插入到 MySQL 数据库中。具体来说,我要求用户在单独的 HTML 表单输入中输入他们的名字和姓氏,我必须将这两个输入连接成一个全名,中间有一个空格(否则“Bob”和“Ross”连接起来就是“BobRoss” ”而不是“鲍勃·罗斯”)。这样做时我不知道从哪里开始。此外,在将全名插入数据库之前,我还需要检查数据库中是否还没有全名,但我已经用名字和姓氏做了这件事,所以应该不会太难。

这是带有表单输入的 HTML 页面:

<html>
<head>
<link rel="stylesheet" href="Site.css">
<?php include("Header.php"); ?>
</div>
</head>

<body>
<div id="main">
<h1>About</h1>

<form action="Insert.php" method="post">
<p>First name:</p><input type="text" name="firstname"><br>
<p>Last name:</p><input type="text" name="lastname"><br>
<p>Age:</p><input type="text" name="age"><br>

<input type="submit">
</form>

<?php include("Footer.php");?>
</div>
</body>
</html>

这是将数据输入数据库的 PHP 页面。目前我正在输入用户的名字、姓氏和年龄,但我需要连接名字和姓氏并确保它不在数据库中,然后将其插入数据库,但我还没有这样做.目前我确保名字是唯一的,我确保姓氏是唯一的,但我不关心年龄是否唯一。

<?php 
$con = mysql_connect("localhost","a7068104_user2","wiseguy1345");
if(!$con) {
die("could not connect to localhost:" .mysql_error());
}

header("refresh:1.5; url=NamesAction.php");

mysql_select_db("a7068104_world") or die("Cannot connect to database");

$name = mysql_real_escape_string($_POST['firstname']);
$query = "SELECT * FROM names_1 WHERE firstname='$name'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
echo "Your name is already in the database and will not be added again!";
}
else {
$query = "INSERT INTO names_1 (firstname) VALUES('$name')";
$result = mysql_query($query);
if($result) {
echo "Your first name was successfully added to the database!";
}
else{
echo "Your first name couldn't be added to the database!";
}
}

$name = mysql_real_escape_string($_POST['lastname']);
$query = "SELECT * FROM names_1 WHERE lastname='$name'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
echo "Your name is already in the database and will not be added again!";
}
else {
$query = "INSERT INTO names_1 (lastname) VALUES('$name')";
$result = mysql_query($query);
if($result) {
echo "Your first name was successfully added to the database!";
}
else{
echo "Your first name couldn't be added to the database!";
}
}

$name = mysql_real_escape_string($_POST['age']);
$query = "INSERT INTO names_1 (age) VALUES('$name')";
$result = mysql_query($query);
if($result) {
echo "Your name was successfully added to the database!";
}
else {
echo "Your name couldn't be added to the database!";
}

mysql_close($con);
?>

<html>
<head>
<link rel="stylesheet" href="Site.css">
<?php include("Header.php"); ?>
</div>
</head>

<body>
<div id="main">
<h1>Names</h1>
<p>You will be redirected back to the <b>Names</b> page in a moment.</p>
<?php include("Footer.php");?>
</div>
</body>
</html>

最佳答案

For a start you shouldn't be using mysql functions as this extension is deprecated as of PHP 5.5.0, and will be removed in the future. I suggest using the new improved PDO library and PDO Prepared Statements, see here.

至于连接,你可以简单地这样做:

$concatenated_name = $_POST['firstname'] . " " . $_POST['lastname'];

这会将名称与中间的空格连接起来。

然后您可以在查询中使用 $concatenated_name

但是我仍然强烈建议您将 PDO 用于所有功能。

关于php - 连接两个 HTML 输入并将它们插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049360/

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