简体   繁体   中英

Inserting multiple var values into same mysql table

This is my first 'built from the ground up' PHP/MySQL project. I've built a very simple HTML form with the same row repeated multiple times:

<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. So if the user only enters data in the first row of fields (project1, status1, etc.) the other 2 insert an empty row.

Is there a way, maybe using 'if isset()', so that I don't have any blank fields in my database?

Build SQL dynamically.

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.

If you want to check the value of posted variable is null, always use empty() method.

For more information: Visit 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);  
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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