将所有INSERT语句合并为一个

时间:2014-04-13 23:32:46

标签: php html mysql sql web

我正在为一个类创建一个页面,我有很多不同的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>

2 个答案:

答案 0 :(得分:1)

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

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

另一个选择是在您的数据库中将Firstname和Lastname标记为Unique列。然后你可以做到以下几点。

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

答案 1 :(得分:0)

这应该完成你要做的事情。

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