User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,689 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,224 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 431 | Replies: 7
Reply
Join Date: Apr 2008
Posts: 8
Reputation: jinx_uk_98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jinx_uk_98 jinx_uk_98 is offline Offline
Newbie Poster

php / sql insert script error

  #1  
May 16th, 2008
Hi guys,

I have this sql statement that is trying to insert data from some fields

<?php
$con = mysql_connect("xxx","xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("xxx", $con);
$sql="INSERT INTO guests (guest_name, guest_email) VALUES";
for($i=0;$i<sizeof($_POST['username']);$i++){

$sql.="('".mysql_real_escape_string($_POST['username'][$i])."','".mysql_real_escape_string($_POST['email'][$i])."')";
if(!$i==sizeof($_POST['username'])){
$sql.=",";
}
}
//
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
?>

but it returns the error

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Cavite,Philippines
Posts: 503
Reputation: ryan_vietnow is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 67
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: php / sql insert script error

  #2  
May 16th, 2008
you have forgotten a period after $sql"

  1. $sql.="INSERT INTO guests (guest_name, guest_email) VALUES";
"death is the cure of all diseases..."
http://ryantetek.wordpress.com
Reply With Quote  
Join Date: Apr 2008
Posts: 8
Reputation: jinx_uk_98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jinx_uk_98 jinx_uk_98 is offline Offline
Newbie Poster

Re: php / sql insert script error

  #3  
May 16th, 2008
cheers Ryan. have added the period but the error still happens
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: php / sql insert script error

  #4  
May 16th, 2008
Print your query and execute it in mysql console/phpmyadmin.. Also show us the query ..
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Apr 2008
Posts: 8
Reputation: jinx_uk_98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jinx_uk_98 jinx_uk_98 is offline Offline
Newbie Poster

Re: php / sql insert script error

  #5  
May 16th, 2008
Originally Posted by nav33n View Post
Print your query and execute it in mysql console/phpmyadmin.. Also show us the query ..


not sure what you mean here. please elaborate?
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: php / sql insert script error

  #6  
May 16th, 2008
  1. $sql="INSERT INTO guests (guest_name, guest_email) VALUES";
  2. for($i=0;$i<sizeof($_POST['username']);$i++){
  3.  
  4. $sql.="('".mysql_real_escape_string($_POST['username'][$i])."','".mysql_real_escape_string($_POST['email'][$i])."')";
  5. if(!$i==sizeof($_POST['username'])){
  6. $sql.=",";
  7. }
  8. }
  9. echo $sql;
What does it print ?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Apr 2008
Posts: 8
Reputation: jinx_uk_98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jinx_uk_98 jinx_uk_98 is offline Offline
Newbie Poster

Re: php / sql insert script error

  #7  
May 16th, 2008
oh i understand now.

it prints :
INSERT INTO guests (guest_name, guest_email) VALUES
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: php / sql insert script error

  #8  
May 16th, 2008
Thats it ? Its not entering the for loop ? Well, then there is the problem. Check your for loop and see if the values are being posted.
Last edited by nav33n : May 16th, 2008 at 6:18 am.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC