Jump to content

need help on inserting mulitple entries


pixeltrace

Recommended Posts

guys,

i need help, i have a form that needs to add 1 to 5 email addresses with its name, company, catergory, etc

my problem is with the emailadded.php because its getting an error on my insert query part.

here is the codes for my emailadd.php page
http://www.sinagtala.net/emailadd.phps

and here is the code for my emailadded.php

http://www.sinagtala.net/emailadded.phps

what could be the problem on my codes?
thanks!
Link to comment
Share on other sites

I suggest you do 5 seperate queries rather than just 1, with error checking for each.  It would also be a good idea to validate all of the emails, not just the first one.

If a query fails, display the SQL so you can see what went wrong.
Link to comment
Share on other sites

The mysql_query() function expects a single query and not multiple queries. Ideally this script should be giving a parsing error p[i][b]arse error, unexpected '\"'[/b][/i] as there is no concatenation operator between the five strings. Probably you can have five calls to mysql_query() function with one query each or a single insert statement to insert all the five records at once - See the Insert query syntax in the mysql manual for the same.
Link to comment
Share on other sites

great!

it work!

but i have one more problem.

i tried adding just two (2) emails. but codes run and it work but the problem is
it also adds the empty fields for query 3, 4, and 5 to the database.

so when i view my table, there are new items which are empty.

how should i fix this one?

thanks!
Link to comment
Share on other sites

I've edited your code so it will run in a loop, including the conditions.
** I've changed all of the script tags into <scrip> becuase SMF won't let me post using this tag. Please edit before testing.

[code]<?php

include '../db_connect2.php';

$name=array();
$company=array();
$email=array();
$category=array();

$i=1;
while($i<=5)
{
$name[$i] = stripslashes($_POST['name'.$i]);
$company[$i] = stripslashes($_POST['company'.$i]);
$email[$i] = stripslashes($_POST['email'.$i]);
$category[$i] = stripslashes($_POST['category'.$i]);

if(empty($name[$i]) || empty($company[$i]) || empty($email[$i]) || empty($category[$i]))
{
die('<scrip language=javascript> alert("You did not submit all of the required information!");</script>');
}

$sql = mysql_query("INSERT INTO emailadd
(full_name, company, email, category)
VALUES('".$name[$i]."', '".$company[$i]."', '".$email[$i]."' , '".$category[$i]."')")
or die ("Error with query ".$i.":<br>".mysql_error());

if(mysql_affected_rows() == 0)
die('<scrip language=javascript> alert("Error adding event");top.location = "email.php?id=2";</script>');

$i++;
}

echo '<scrip language=javascript> alert("New email address has been added for the mailing list!");top.location = "email.php?id=2";</script>';


?>[/code]

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.