简体   繁体   English

将多个var值插入相同的mysql表

[英]Inserting multiple var values into same mysql table

This is my first 'built from the ground up' PHP/MySQL project. 这是我的第一个“从头开始”的PHP / MySQL项目。 I've built a very simple HTML form with the same row repeated multiple times: 我建立了一个非常简单的HTML表单,其中同一行重复了多次:

<h2 style="margin-top:0">Current Projects</h2>      
    Project: <input name="project1" type="text"  size="40" value="1" /> 
    Status: <input name="status1" type="text" size="40" value="1" /> 
    Estimated Completion: <input name="estCompletion1" type="text" size="12" value="1" /><br /> 

    Project: <input name="project2" type="text" size="40" value="2" /> 
    Status: <input name="status2" type="text" size="40" value="2" /> 
    Estimated Completion: <input name="estCompletion2" type="text" size="12" value="2" /><br />

    Project: <input name="project3" type="text" size="40" value="3" /> 
    Status: <input name="status3" type="text" size="40" value="3" /> 
    Estimated Completion: <input name="estCompletion3" type="text" size="12" value="3" /><br />

I'm trying to write all of these to the same database table at the same time, but on different rows. 我正在尝试将所有这些都同时写入同一数据库表,但要放在不同的行上。

$sql = "INSERT INTO current_project (date, est_completion, project_name, status)
VALUES  (NOW(), '$estCompletion1', '$project1', '$status1'),
        (NOW(), '$estCompletion2', '$project2', '$status2'),
        (NOW(), '$estCompletion3', '$project3', '$status3')";

The problem with doing it like this is that it inserts the data from the variable, even if it's null. 这样做的问题在于,即使变量为null,它也会从变量中插入数据。 So if the user only enters data in the first row of fields (project1, status1, etc.) the other 2 insert an empty row. 因此,如果用户仅在字段的第一行(项目1,状态1等)中输入数据,则其他2插入一个空行。

Is there a way, maybe using 'if isset()', so that I don't have any blank fields in my database? 有没有办法,也许使用'if isset()',这样我的数据库中没有空白字段?

Build SQL dynamically. 动态构建SQL。

Something like this: 像这样:

$inserts = Array();
for($i=1; $i<=3; $i++) {
    if (!$_POST["project".$i] && !$_POST["status".$i] && !$_POST["estCompletion".$i]) continue;
    $inserts[] = "(NOW(), '".$_POST["estCompletion".$i]."', '".$_POST["project".$i]."', '".$_POST["status".$i]."')";
}
if (Count($inserts)>0) {
    $sql = "INSERT INTO current_project (date, est_completion, project_name, status) 
    VALUES (" . implode("), (", $inserts) . ")";
}

At this point $sql should have full SQL query only with those rows where all three fields were submitted. 此时,$ sql仅应具有提交了所有三个字段的那些行的完整SQL查询。

If you want to check the value of posted variable is null, always use empty() method. 如果要检查发布的变量的值为null,请始终使用empty()方法。

For more information: Visit php.net 有关更多信息:访问php.net

 for($i=1; $i<=3; $i++) {
        $estCompletion= $_POST["estCompletion".$i];
        $project= $_POST["project".$i];
        $status= $_POST["status".$i];

        if (!empty($estCompletion) && !empty($project) && !empty($status))
        {       
            $sql = "INSERT INTO current_project (date, est_completion, project_name, status) VALUES  (NOW(), '".$estCompletion."', '".$project."', '".$status."')";
        }

    unset($estCompletion, $project, $status);  
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM