将所有INSERT语句合并为一个语句


Combining all INSERT statements into one

我正在为一个类创建一个页面,我有很多不同的while循环和许多不同的INSERT语句。直到我发现它们的输入不正确,我才认为这会有问题。它们都是输入的,但实际情况是,它们都是在单独的行中输入的,而不是在单独的列中作为一个条目。我需要它,以便将所有INSERT语句组合为一个语句,同时仍在检查以确保数据库中还没有全名。请帮我重写代码,这样它就可以做到这一点。

以下是需要重写的代码,以便只有一条INSERT语句:

<?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 first 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 last 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['firstname'] . " " . $_POST['lastname']);
$query = "SELECT * FROM names_1 WHERE fullname='$name'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
        echo "Your full name is already in the database and will not be added again!";
}
else {
        $query = "INSERT INTO names_1 (fullname) VALUES('$name')";
        $result = mysql_query($query);
}
$age = mysql_real_escape_string($_POST['age']);
    $query = "INSERT INTO names_1 (age) VALUES('$age')";
    $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>

当插入一行多列时,您可以将两组键和值组合起来,如下所示:

INSERT INTO names_1 (firstname, lastname) VALUES ('John', 'Smith')

另一种选择是在数据库中将名字和姓氏都标记为"唯一"列。然后你可以做以下操作。

INSERT INTO names_1 (firstname, lastname) VALUES ('John', 'Smith') ON DUPLICATE KEY UPDATE names_1 SET x="blah" WHERE blah

http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

这应该可以完成您要做的事情。

<?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");
$fullname = mysql_real_escape_string($_POST['firstname'] . " " . $_POST['lastname']);
$query = "SELECT * FROM names_1 WHERE fullname='" . $fullname . "'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ){
        echo "Your full name is already in the database and will not be added again!";
}
else {
    $query = "INSERT INTO names_1 (firstname, lastname, fullname, age) VALUES('" .     $_POST['firstname'] . "','" . $_POST['lastname'] . "','" . $fullname . "'," . $_POST['age'] . ")";
    $result = mysql_query($query);
}

mysql_close($con);
?>
<html>
    <head>
        <link rel="stylesheet" href="Site.css"/>
        <?php include("Header.php"); ?>
    </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>