Ricky Nkonya
Ricky Nkonya

Reputation: 15

Avoiding empty data inserted into mysql

I have four texts fields in my form that i need them to be inserted into my database,but the four fields are optional to either they should all be filled or just part of them,my problem is that in case the user just fills the two fields and leaves the other two empty,then the filled data should be inserted into mysql and the other blank fields should not be inserted.

This is what i have.

Artist:<input type="text" name="artist" />

Artist:<input type="text" name="artist1" />

Artist:<input type="text" name="artist2" />

Artist:<input type="text" name="artist3" />

//my php code

$sql="INSERT INTO artists (name) VALUES ('$artist'),('$artist1'),('$artist2'),('$artist3')";
mysql_query($sql);

but whenever i run this query it inserts all the four fields.Any help please. And is it relevant to use artist,artist1,artist2,artist3 in the four artist fields or i can just use maybe artist[] for the name part of all the four artist's fields i have?

Upvotes: 0

Views: 445

Answers (4)

Louis Huppenbauer
Louis Huppenbauer

Reputation: 3714

You could configure your input-fields to return an array, iterate over that array and create the query dynamically.

$values = array();
foreach($artists AS $artist) {
    if(!empty($artist)) {
        $values[] = "('".mysqli_real_escape_string($artist)."')";
    }
}
if(count($values) > 0) {
    $sql = 'INSERT INTO artists (name) VALUES ' . implode(',', $values);
}

Should work. But please note that the mysql_* functions are deprecated and you are encouraged to use either the mysqli_* or the PDO functions.

Upvotes: 1

neythz
neythz

Reputation: 74

make your input field to have same name as array :

Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />

loop your artist

foreach($_POST['artist'] as $artist){
    //insert to db if not null
    if(trim($artist) != null){
        $sql="INSERT INTO artists (name) VALUES ('$artist')";
        mysql_query($sql);
    }
}

Upvotes: 0

Prashanth
Prashanth

Reputation: 13

This isn't a good idealogy. You can create a table with two columns for it. you table can be like

Create table Table_name(
Uid int(5) Auto_increment,
artistName varchar(20) NOT NULL
)

Primary key(Uid)

Now, you can use the query as:

<?php    
foreach($_GET as $name)
    mysql_query(insert into Table_Name(ArtistName) values('$name'));
?>

declaring the column as NOT NULL willnot allow to insert NULL values.

Upvotes: 0

Corbin
Corbin

Reputation: 33437

Rename your inputs to have name="artist[]". Not strictly necessary, but much easier.

Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />
Artist:<input type="text" name="artist[]" />

Then process them and build your query as necessary:

$rawArtists = (isset($_POST['artist']) && is_array($_POST['artist'])) ? $_POST['artist'] : array();

$artists = array();

foreach ($rawArtists as $artist) {
    if (is_string($artist) && strlen($artist)) {
        $artists[] = $artist;
    }
}

if (count($artists)) {

    //This could easily be a loop...  
    //I just enjoy abusing lambdas from time to time :)
    $artistVals = array_map(function ($art) {
        return "('" . mysql_real_escape_string($art) . "')";
    }, $artists);

    $vals = implode(',', $artistVals);

    $query = "INSERT INTO artists (name) VALUES $vals";

}

Upvotes: 1

Related Questions