douxiong5438 2014-04-13 23:32
浏览 294
已采纳

将所有INSERT语句合并为一个

I'm creating a page for a class and I have lots of different while loops with lots of different INSERT statements. I didn't think there would be a problem with that until I discovered that they weren't being inputed right. They were all inputed but what happened was that they were all inputed in separate rows as opposed to being one entry across their individual columns. I need it so that all of the INSERT statements are combined into one while still checking to make sure that the full name isn't yet in the database. Please help me rewrite my code so that it can do just that.

Here is the code that needs to be rewritten so that it just has one INSERT statement:

<?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条回答 默认 最新

  • dst8922 2014-04-14 00:05
    关注

    When inserting a row with multiple columns you can just combine the two groups of keys and values like so:

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

    The other alternative is in your database mark both the Firstname and Lastname as Unique columns. Then you can do the following.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog