Click to See Complete Forum and Search --> : PHP Insert to MySQL via Form


franknu
06-07-2006, 11:16 AM
I am just having some trouble here

i dont dont weather i should use( ", ', ` or and ) on my insert comand i see it in many diffrent ways on the web and on books

this is the error code
Parse error: parse error in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 25




<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);



$query= "insert into First Name:('"First Name`=`First Name`and `Last Name:`=`Last Name:`
and `Address`=`Address` and `State`=State` and `City`=`City` and `Tel`=`Tel` and
`Email`=`Email` and `Comments`=`Comments` and `Position:`=`Position:`
and `Status`=`Status`");"
,$db) or die(mysql_error());


$result=mysql_query($query);

if($result)

echo mysql_affected_rows()." Business Owner Inserted.';


?>



any other way i should be doing it this
thank you .

chazzy
06-07-2006, 11:55 AM
INSERTS look like this:

INSERT INTO table(col1,col2,...,coln) VALUES('val1','val2',...,'valn');

franknu
06-07-2006, 07:23 PM
I did some changes and i was still getting a parser error

this is the code


<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addlashes($State);
$City= addlashes($City);
$Zip= addlashes($Zip);
$Tel= addlashes($Tel);
$Email= addlashes($Email);

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);

if(!$db)

{
echo " Error: could not connect to database.";

exit;
}

$mysql_select_db("contact");

$sql = 'INSERT INTO `First Name:` ( `First ` , `Last ` , `Address:` , `State` , `City` , `Zip` , `Tel` , `Email` , `Comments` , `Position:` , `Status` ) ';
$sql .= 'VALUES ( \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\' );';
$sql .= '';

$result=mysql_query($query);

if($result)

echo mysql_affected_rows()." Business Owner Inserted.';


?>



the error messages is showing outsite the tags

Parse error: parse error in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 64
i dont have line 64

thank you

chazzy
06-07-2006, 07:51 PM
don't use single quotes to encapsulate your string in php when using sql, it's difficult to read (so remove the \'s). also you don't need the ; when going through php.

why do you connect to your database twice?

it looks like you might have some spacing issues. try to cut down and use {} whenever possible.

franknu
06-07-2006, 08:11 PM
ok, i fixed i only have one little problem when i send the data via a form, there is no info saved on the databse there is only empty rows, can anyone please tell me why is that here are the codes


<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addslashes($State);
$City= addslashes($City);
$Zip= addslashes($Zip);
$Tel= addslashes($Tel);
$Email= addslashes($Email);



if(!$db)

{
echo " Error: could not connect to database.";

exit;
}



$sql = "INSERT INTO `First Name:` ( `First ` , `Last ` , `Address` , `State` , `City` , `Zip` , `Tel` , `Email` , `Comments` , `Position:` , `Status` ) ";
$sql .= "VALUES ( \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\" );";
$sql .= "";

$result=mysql_query($sql);


if($result)
{
echo mysql_affected_rows()." Business Owner Inserted.";
}



?>

chazzy
06-07-2006, 08:35 PM
this is completely invalid and doesn't make sense.


$sql = "INSERT INTO `First Name:` ( `First ` , `Last ` , `Address` , `State` , `City` , `Zip` , `Tel` , `Email` , `Comments` , `Position:` , `Status` ) ";
$sql .= "VALUES ( \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\" );";
$sql .= "";


As I pointed out, don't use single quotes, but then you just replaced with "'s >_<

use something like this


$sql = "INSERT INTO `First Name:` ( `First ` , `Last ` , `Address` , `State` , `City` , `Zip` , `Tel` , `Email` , `Comments` , `Position:` , `Status` ) VALUES ('".$First."','".$Last."','".$Address/* and so on, you should get the picture.*/)";

franknu
06-08-2006, 05:14 PM
i want to insert data into my database i done some changes but sometimes. it goes through but i dont record any data....
help please

<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addslashes($State);
$City= addslashes($City);
$Zip= addslashes($Zip);
$Tel= addslashes($Tel);
$Email= addslashes($Email);



if(!$db)

{
echo " Error: could not connect to database.";

exit;
}



$sql =INSERT INTO `First Name:` ("First" , "Last", "Address", "State", "City", "Zip", "Tel","Email", "Comments" , "Position", "Status");
VALUES ( '".$First."','".$Last."','".$Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Position."','".$Status.");
$sql="";

$result=mysql_query($sql);


if($result)
{
echo mysql_affected_rows()." Business Owner Inserted.";
}



?>



this is the error

Parse error: parse error in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 48

chazzy
06-08-2006, 05:29 PM
$sql = "INSERT INTO `First Name:` ( `First ` , `Last ` , `Address` , `State` , `City` , `Zip` , `Tel` , `Email` , `Comments` , `Position:` , `Status` ) VALUES ('".$First."','".$Last."','".$Address/* and so on, you should get the picture.*/)";


Please note the order in which I use the `,",', and .

franknu
06-08-2006, 07:47 PM
well, i cant see what the problems is maybe i am missing something i am still getting a parser error or no data is intruduce into the database can anyone please pinpoint the problem




<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addslashes($State);
$City= addslashes($City);
$Zip= addslashes($Zip);
$Tel= addslashes($Tel);
$Email= addslashes($Email);



if(!$db)

{
echo " Error: could not connect to database.";

exit;
}



$sql="INSERT INTO `First Name:` (`First`,`Last`,`Address`,`State`,`City`, `Zip`,`Tel`, `Email`,`Comments`,`Position`,`Status`)
VALUES ('".$First."','".$Last."','".$Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Position."','".$Status.");
$result=mysql_query($sql);


if($result)

{
echo mysql_affected_rows()." Business Owner Inserted.";
}



?>


this is the error
Parse error: parse error in /home/httpd/vhosts/mass-ad.com/httpdocs/insert_data.php on line 56
thank you for your help

chazzy
06-08-2006, 08:04 PM
in that last post, the error's right here:


."','".$Status.");


should be

."','".$Status."'");

hyperlisk
06-08-2006, 08:34 PM
You know, I don't see anywhere that you are collecting the data from the $_POST or $HTTP_POST_VARS variable... You need to get the data before you can insert it...

franknu
06-08-2006, 09:38 PM
Still getting parser error



<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addslashes($State);
$City= addslashes($City);
$Zip= addslashes($Zip);
$Tel= addslashes($Tel);
$Email= addslashes($Email);



if(!$db)

{
echo " Error: could not connect to database.";

exit;
}



$sql="INSERT INTO `First Name:`(`First`,`Last`,`Address`,`State`,`City`, `Zip`,`Tel`, `Email`,`Comments`,`Position`,`Status`)
VALUES ('".$First."','".$Last."','".$Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Position."','".$Status."'");

$result=mysql_query($sql);


if($result)

{
echo mysql_affected_rows()." Business Owner Inserted.";
}



?>



maybe i am looking at it to hard that it way i cant see the error
thank you guys

hyperlisk
06-08-2006, 09:59 PM
Your SQL statement is wrong... use this:

$sql="INSERT INTO `First Name:`(`First`,`Last`,`Address`,`State`,`City`, `Zip`,`Tel`, `Email`,`Comments`,`Position`,`Status`)
VALUES ('".$First."','".$Last."','".$Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Position."','".$Status."')";


And also, I don't think "First Name:" is a valid table name.

franknu
06-08-2006, 10:07 PM
now i only get a blank page after i summit and the html form and no info is being recorded in the database



<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);





$First = addslashes($First);
$Last= addslashes($Last);
$Address = addslashes($Address);
$State= addslashes($State);
$City= addslashes($City);
$Zip= addslashes($Zip);
$Tel= addslashes($Tel);
$Email= addslashes($Email);



if(!$db)

{
echo " Error: could not connect to database.";

exit;
}



$sql="INSERT INTO `First Name:`(`First`,`Last`,`Address`,`State`,`City`, `Zip`,`Tel`, `Email`,`Comments`,`Position`,`Status`)
VALUES ('".$First."','".$Last."','".$Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Position."','".$Status."')";

$result = mysql_query($sql);


if($result)

{
echo mysql_affected_rows()." .Business Owner Inserted.";
}



?>

hyperlisk
06-08-2006, 10:10 PM
Put this after $result = mysql_query($sql);



echo mysql_error();

franknu
06-08-2006, 10:15 PM
Unknown column 'First' in 'field list'


this is the error message that i am getting now i have the row First int he colum First name:

help

hyperlisk
06-08-2006, 10:18 PM
OK, NONE of your tables or rows should have spaces in the names. If they do, then you need to fix them. Just use an underscore (_) instead, then update your code.

franknu
06-09-2006, 07:37 AM
Everything works Great thank you, it seems my problem were mostly at the database
thank you