0

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..................

2
Contributors
10
Replies
24
Views
4 Years
Discussion Span
Last Post by pritaeas
0

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?

0

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'

0
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).

0

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

0

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')");

Edited by pritaeas

0

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....

0

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

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.