Member Avatar for philip.s

Hi guy's,

I am missing something, what on earth is wrong with this script, I can not get it to insert all my values into a table.

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="1234"; // Mysql password
$db_name="database"; // Database name
$tbl_name="input table"; // Table name

$que = mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name");

$query  = "SELECT value FROM $tbl_name";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{

$value = row['value']

//CODE TO GET VALUES

//CODE TO GET VALUES

$con = mysql_connect("localhost","root","1234");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$sql="INSERT INTO finaltable (value0, value1, value2)
VALUES
('$value0','value1','$value2')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con);


}

mysql_close($que)

The code above should get a value from a table, give it to a script in the "while" loop and store it in a final table, When the script is executed i get 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 'blah blah blah' at line 3"

Looks like you missed the $ in front of value1

$sql="INSERT INTO finaltable (value0, value1, value2)
VALUES
('$value0','value1','$value2')";

First the mini-lecture: If you are going to be serious about writing code that works, then you need to be able to research and to debug. Not much evidence of either one here.

This may not be your only problem but one thing that I noticed is that you are using a table name of input table. It keeps things simpler if you use names with alpha-numeric characters and underscores (only). If you insist on using names with other characters, including blanks, then you must enclose them in back-tick characters (`xxxx yyyy`) wherever you use them. Start with that and if it still doesn't work then do some debugging and figure out exactly where it is failing.

Member Avatar for philip.s

Thanks Guy's
All great suggestions, but the value's like database names and value names (sorry about the "$" mistake) are examples and not what is used in the actual code, Would you agree that the format of the curly brackets and placement of the loop, and placement of the script that generates the final value's are correct? I also think that it might have something to do with the single quotes around the variables, because some times value's get inserted and other times not.
Thanks guy's

Member Avatar for philip.s

Thanks Guy's
All great suggestions, but the value's like database names and value names (sorry about the "$" mistake) are examples and not what is used in the actual code, Would you agree that the format of the curly brackets and placement of the loop, and placement of the script that generates the final value's are correct? I also think that it might have something to do with the single quotes around the variables, because some times value's get inserted and other times not.
Thanks guy's

This may seem stupid, but have you tried:

$sql="INSERT INTO finaltable (`value0`, `value1`, `value2`)
VALUES
('$value0','$value1','$value2')";

some times value's get inserted and other times not.

Probably because the inputed text has a single quote in it, which will mess up the sql statement. I'd suggest addslashes($value) or removing quotes from being entered.

I hope this was useful! Good luck!

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.