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 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决