dopnpoh056622 2014-04-13 22:41
浏览 158
已采纳

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

I'm creating this web page for this class that I'm in and for it I need to concatenate two separate HTML form inputs with a space in between and insert them into a MySQL database. Specifically I ask the user for their first name and their last name in separate HTML form inputs and I have to concatenate those two input into a full name with a space in between (or else "Bob" and "Ross" concatenated would be "BobRoss" instead of "Bob Ross"). I don't know where to start when doing that. Also I need to check that the full name isn't already in the database before inserting it into the database, but I'm already doing that with the first name and last name so that shouldn't be too hard.

Here is the HTML page with the form inputs:

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

And here is the PHP page where it inputs the data into the database. Currently I'm inputing the user's first name, last name, and age, but I need to concatenate the first and last name and make sure it isn't in the database and then insert it into the database and I haven't done that. Currently I make sure that the first name is unique, I make sure that the last name is unique, but I don't care whether the age is unique or not.

<?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>
  • 写回答

2条回答 默认 最新

  • doufendi9063 2014-04-13 23:04
    关注

    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.

    As for the concatenation, you could simply do it like this:

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

    This would concatenate the names with a space in between.

    You can then use $concatenated_name in your queries.

    However I still strongly recommend you use PDO for all your functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)