Hi could someone help me out here. I have a frm that has multiple entries. Each entry has 'artist' 'linl' and 'link text'. link and linktext are optional. My code at the minute needs all to be filled in, if I leave link and link trext blank I get errors.

Here my php code.

 <?php
    include '../inc/connect.php';
    if (isset($_POST['submit'])){  
       foreach($_REQUEST['artist'] as $aartist){
          if ($aartist != ''){
           $artist = mysql_real_escape_string($aartist);
              foreach($_REQUEST['alink'] as $alink){
                if ($alink != ''){
                 $link = mysql_real_escape_string($alink);
                 foreach($_REQUEST['alinktext'] as $alinktext){
                  if ($alinktext != ''){
                    $linktext = mysql_real_escape_string($alinktext);
                  }
                  }
                 }
                 }
                 mysql_query("INSERT INTO artists  VALUES ('', '$artist', '$link', '$linktext')");
              }
              }
           }
        ?>

This is my form

<form class="form1" action="artistadd.php" method="post" enctype="multipart/form-data" name="artist_add_form" id="artist_add_form">
    <p><?php for($i=0; $i<8; $i++): ?>
    <span class='text'><b>Artist</b></span> <input type=text name="artist[]">
    <span class='text'><b>URL</b></span> <input type=text name="alink[]">
    <span class='text'><b>Link Text</b></span> <input type=text name="alinktext[]"><br />
    <?php endfor; ?></p>
    <input type="submit" name="submit" value="Upload Links" />       
    <input name="submitted_form" type="hidden" name="submitted_form" value="artistss_upload_form" />
</form>

Thanks for looking..................

if I leave link and link trext blank I get errors.

What errors?

Apart from that, your construct with three nested foreach loops is weird. You know you have 8 fields, why not use a single for loop?

These are the errors

' Notice: Undefined variable: link in C:\wamp\www\needlegangstas\admin\artistadd.php on line 51'
'Notice: Undefined variable: linktext in C:\wamp\www\needlegangstas\admin\artistadd.php on line 51'

How would I go about putting all that in a single for loop?

for ($i=0; $i<8; $i++)
{
    if (!empty($_POST['artist'][$i]))
    {
        $artist = mysql_real_escape_string($_POST['artist'][$i]);
        $alink = mysql_real_escape_string($_POST['alink'][$i]);
        $alinktext = mysql_real_escape_string($_POST['alinktext'][$i]);
        mysql_query("INSERT INTO artists VALUES ('', '$artist', '$link', '$linktext')");
    }
}

It will only skip the ones where artist is empty (assuming that is what you want).

Thanks, but how would I make it so that link and linktext could e left empty on the form, withou giving me the errors?

You still have the same errors? You can do this:

$alink = (isset($_POST['alink'][$i]) and !empty($_POST['alink'][$i])) ? mysql_real_escape_string($_POST['alink'][$i]) : '';
$alinktext = (isset($_POST['alinktext'][$i]) and !empty($_POST['alinktext'][$i])) ? mysql_real_escape_string($_POST['alinktext'][$i]) : '';

Or perhaps it's a typo here:

mysql_query("INSERT INTO artists VALUES ('', '$artist', '$alink', '$alinktext')");

Again thanks. I just done

if (!empty($_POST['alink'][$i])){
  $alink = mysql_real_escape_string($_POST['alink'][$i]);
}
else{
  $alink = NULL;
}

So if link or linktext are empty they have NULL values....

So if link or linktext are empty they have NULL values....

Are you sure the query is not inserting the string 'NULL' now?

Its not, its inserting nothing into them fields! i have the links and linktext fields in my table set to NUll-yes..................

Okay, just checking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.