View Full Version : Php/MySql problem


flaviussirop
02-25-2006, 08:02 AM
People, I am telling you, it just WON'T WORK! I am desperate. I am trying to insert some values in a table. I got the MySql query(converted to PHP) directly from the Apache server of my host and it looks like this:

$sql = 'INSERT INTO `poezii` (`nr`, `fname`, `lname`, `email`, `addr1`, `addr2`, `city`, `state`, `zip`, `country`, `phone`, `title`, `poem`, `date`) VALUES (NULL, \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'10\', \'11\', \'12\', \'13\');';

Now this one works, and inserts me all the fields Null,1,2,3,..13. But only works with constants, as here. If I want to insert a variable like $fname, it will only insert me the string $fname and not its content.
I have tried various styles like '".$fname."' - it just won't work! I am desperate!
:fuct:

sparrowhawk
02-25-2006, 10:46 AM
Can you please post the complete code including the php function you are using to perform the insert? I'm sure that I can help you if you include that whole portion of your script.

flaviussirop
02-25-2006, 10:57 AM
include("connect.php");
$sql = 'INSERT INTO `poezii` (`nr`, `fname`, `lname`, `email`, `addr1`, `addr2`, `city`, `state`, `zip`, `country`, `phone`, `title`, `poem`, `date`) VALUES (NULL, \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'10\', \'11\', \'12\', \'13\');';
mysql_query($sql) or die("Syntax error");
echo "$fname ";
echo "$sql";

that should be it.. it works for inserting constants, but instead of variables content, it inserts the variable's name like $variable(inside the database)

sparrowhawk
02-26-2006, 04:19 AM
[QUOTE=flaviussirop]include("connect.php");
$sql = 'INSERT INTO `poezii` (`nr`, `fname`, `lname`, `email`, `addr1`, `addr2`, `city`, `state`, `zip`, `country`, `phone`, `title`, `poem`, `date`) VALUES (NULL, \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'10\', \'11\', \'12\', \'13\');';
mysql_query($sql) or die("Syntax error");
echo "$fname ";
echo "$sql";

that should be it.. it works for inserting constants, but instead of variables content, it inserts the variable's name like $variable(inside the database)[/QUOTE]

Well OK - Here is the code for what I think you are trying to accomplish. I changed the single quotes to double quotes to avoid escaping all the quotes for your strings.


/*I'M ASSUMING THE NAMES OF YOUR VARIABLES - REPLACE MINE WITH THE REAL ONES */

$sql = "INSERT INTO `poezii` (`nr`, `fname`, `lname`, `email`, `addr1`, `addr2`, `city`, `state`, `zip`, `country`, `phone`, `title`, `poem`, `date`) VALUES (" . $nr . ", '" . $fname . "', '" . $lname . "', '" . $email . "', '" . $addr1 . "', '" . $addr2 . "', '" . $city . "', '" . $state . "', '" . $zip . "', '" . $country . "', '" . $phone . "', '" . $title . "', '" . $poem . "', '" . $date . "')";
mysql_query($sql) or die("Syntax error");

sparrowhawk
02-26-2006, 05:07 AM
Here's an easier way to work with the data. There are others as well. I included a lot of comments to try to point out what I was doing. You can of course delete these.


<?php
//INCLUDE THE DATABASE CONNECTION
include("connect.php");

/*
THIS IS A GOOD FUNCTION FOR DATABASE PREP FROM DW MX
IT WILL EASILY ESCAPE NECESSARY CHARACTERS BEFORE AN INSERT OR UPDATE.
YOU ONLY NEED TO INCLUDE THIS ONCE IN EACH FILE YOU ARE WORKING ON
OR YOU CAN ADD IT TO THE PHP PATH AND CALL IT FROM ANY PLACE

YOU CAN DEFINITELY INCLUDE THIS FUNCTION FROM ANOTHER FILE TO KEEP CODE FROM BLOATING
*/
function db_prep($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

/*
I'M ASSUMING THE NAMES OF YOUR VARIABLES - REPLACE MINE WITH THE REAL ONES
I'M NOT SURE WHAT DATATYPE nr IS - I AM ASSUMING THAT IT IS AN INTEGER (PRIMARY KEY)
CHANGE IT TO THE CORRECT DATATYPE IF ITS NOT AN INTEGER
LOOK UP THE sprintf FUNCTION AT php.net - IT MAKES WORKING WITH SQL STRINGS MUCH EASIER
AS YOU CAN SEE IT LEAVES PLACEHOLDERS FOR THE VALUES THAT ARE DEFINED IN THE SECOND
ARGUEMENT OF THE FUNCTION.
*/
$sql = sprintf("INSERT INTO `poezii` (`nr`, `fname`, `lname`, `email`, `addr1`, `addr2`, `city`, `state`, `zip`, `country`, `phone`, `title`, `poem`, `date`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
db_prep($nr, "int"),
db_prep($fname, "text"),
db_prep($lname, "text"),
db_prep($email, "text"),
db_prep($addr1, "text"),
db_prep($addr2, "text"),
db_prep($city, "text"),
db_prep($state, "text"),
db_prep($zip, "text"),
db_prep($country, "text"),
db_prep($phone, "text"),
db_prep($title, "text"),
db_prep($poem, "text"),
db_prep($date, "date"));
/*
WHILE THERE'S A LITTLE MORE CODE IT DEFINITELY MAKES IT EASIER TO WORK WITH LARGE
INSERTS AND UPDATE. HOPE THIS HELPS
*/

//EXECUTE THE SQL
mysql_query($sql) or die("Syntax error");
?>

flaviussirop
02-26-2006, 01:07 PM
gee.. thanks for the effort man. I sure hope this will work. you're cool!