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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化