Jump to content

[SOLVED] Mysql query using php question


ballhogjoni

Recommended Posts

Can someone tell me whats wrong with this query. Its saving the data but it skips the fname column and starts with the lname column instead.

 

$query = "INSERT INTO contact_n_questions (fname, lname, position, email, state, areacode, prefix, linenumber, question1, question2, question2q1, question2q2, question2q3, question2q4, question2q5, question2q6, question3, question4q1, question4q2, question4q3, question4q4, question4q5, question5q1, question5q2, question5q3, question5q4, question5q5, question5q6, question6, question7, question8, question9, tandc, comments) VALUES ('','$fname','$lname','$position','$email','$state','$areacode','$prefix','$linenumber','$tandc','$question1','$question2','$question2q1','$question2q2','$question2q3','$question2q4','$question2q5','$question2q6','$question3','$question4q1','$question4q2','$question4q3','$question4q4','$question4q5','$question5q1','$question5q2','$question5q3','$question5q4','$question5q5','$question5q6','$question6','$question7','$question8','$question9')";

Link to comment
Share on other sites

No, you don't.  As long as the default value for an AUTO_INCREMENT column is NULL, then it will increment itself.  Or, if you do, indeed, need that empty string to do so, then you also have to include the AUTO_INCREMENT column in the column list.

Link to comment
Share on other sites

Ok this is what my query looks like now. I am running into a new problem, none of the data is being written to my db. I have echoed the variables and the values are being passed but for some reason it wont write to the db.

 

$query = "INSERT INTO contact_n_questions VALUES ('','$fname','$lname','$position','$email','$state','$areacode','$prefix','$linenumber','$question1','$question2','$question2q1','$question2q2','$question2q3','$question2q4','$question2q5','$question2q6','$question3','$question4q1','$question4q2','$question4q3','$question4q4','$question4q5','$question5q1','$question5q2','$question5q3','$question5q4','$question5q5','$question5q6','$question6','$question7','$question8','$question9','$tandc')";

Link to comment
Share on other sites

No. The page pulls up fine when I'm passing the variables. Heres all my code.

 

<?php
$username="xxxxx";
$password="xxxxx";
$database="xxxxx";

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$position=$_POST['position'];
$email=$_POST['email'];
$state=$_POST['state'];
$areacode=$_POST['areacode'];
$prefix=$_POST['prefix'];
$linenumber=$_POST['linenumber'];
$question1=$_POST['question1'];
$question2=$_POST['question2'];
$question2q1=$_POST['question2q1'];
$question2q2=$_POST['question2q2'];
$question2q3=$_POST['question2q3'];
$question2q4=$_POST['question2q4'];
$question2q5=$_POST['question2q5'];
$question2q6=$_POST['question2q6'];
$question3=$_POST['question3'];
$question4q1=$_POST['question4q1'];
$question4q2=$_POST['question4q2'];
$question4q3=$_POST['question4q3'];
$question4q4=$_POST['question4q4'];
$question4q5=$_POST['question4q5'];
$question5q1=$_POST['question5q1'];
$question5q2=$_POST['question5q2'];
$question5q3=$_POST['question5q3'];
$question5q4=$_POST['question5q4'];
$question5q5=$_POST['question5q5'];
$question5q6=$_POST['question5q6'];
$question6=$_POST['question6'];
$question7=$_POST['question7'];
$question8=$_POST['question8'];
$question9=$_POST['question9'];
$tandc=$_POST['tandc'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contact_n_questions VALUES ('','$fname','$lname','$position','$email','$state','$areacode','$prefix','$linenumber','$question1','$question2','$question2q1','$question2q2','$question2q3','$question2q4','$question2q5','$question2q6','$question3','$question4q1','$question4q2','$question4q3','$question4q4','$question4q5','$question5q1','$question5q2','$question5q3','$question5q4','$question5q5','$question5q6','$question6','$question7','$question8','$question9','$tandc')";


mysql_query($query);
mysql_close();


echo $fname;
echo $lname;


?>
<html>
<head>
<meta http-equiv="refresh" content=".0;url=http://www.xxxxxx/thankyou.php"
</head>
<body>
</body>
</html>

Link to comment
Share on other sites

hi,

 

can you try this and see if it works:

 

$query = "INSERT INTO contact_n_questions VALUES ('$fname','$lname','$position','$email','$state','$areacode','$prefix','$linenumber','$question1','$question2','$question2q1','$question2q2','$question2q3','$question2q4','$question2q5','$question2q6','$question3','$question4q1','$question4q2','$question4q3','$question4q4','$question4q5','$question5q1','$question5q2','$question5q3','$question5q4','$question5q5','$question5q6','$question6','$question7','$question8','$question9','$tandc')";

Link to comment
Share on other sites

// ...
mysql_query($query);
if (mysql_errno()) echo "Database error: ",mysql_error();
mysql_close();
// ...

 

And the variable reassignment seems a little ... cumbersome ... at best.  You could use $_POST directly, or better yet, use a sprintf and mysql_real_escape_string.

 

But check your return value from MySQL first.

Link to comment
Share on other sites

It could be cleaner, but try this for now:

$query = sprintf("INSERT INTO contact_n_questions (fname, lname, position, email, state, areacode, prefix, linenumber, question1, question2, question2q1, question2q2, question2q3, question2q4, question2q5, question2q6, question3, question4q1, question4q2, question4q3, question4q4, question4q5, question5q1, question5q2, question5q3, question5q4, question5q5, question5q6, question6, question7, question8, question9, tandc, comments) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
	mysql_real_escape_string($_POST['fname']),
	mysql_real_escape_string($_POST['lname']),
	mysql_real_escape_string($_POST['position']),
	mysql_real_escape_string($_POST['email']),
	mysql_real_escape_string($_POST['state']),
	mysql_real_escape_string($_POST['areacode']),
	mysql_real_escape_string($_POST['prefix']),
	mysql_real_escape_string($_POST['linenumber']),
	mysql_real_escape_string($_POST['question1']),
	mysql_real_escape_string($_POST['question2']),
	mysql_real_escape_string($_POST['question2q1']),
	mysql_real_escape_string($_POST['question2q2']),
	mysql_real_escape_string($_POST['question2q3']),
	mysql_real_escape_string($_POST['question2q4']),
	mysql_real_escape_string($_POST['question2q5']),
	mysql_real_escape_string($_POST['question2q6']),
	mysql_real_escape_string($_POST['question3']),
	mysql_real_escape_string($_POST['question4q1']),
	mysql_real_escape_string($_POST['question4q2']),
	mysql_real_escape_string($_POST['question4q3']),
	mysql_real_escape_string($_POST['question4q4']),
	mysql_real_escape_string($_POST['question4q5']),
	mysql_real_escape_string($_POST['question5q1']),
	mysql_real_escape_string($_POST['question5q2']),
	mysql_real_escape_string($_POST['question5q3']),
	mysql_real_escape_string($_POST['question5q4']),
	mysql_real_escape_string($_POST['question5q5']),
	mysql_real_escape_string($_POST['question5q6']),
	mysql_real_escape_string($_POST['question6']),
	mysql_real_escape_string($_POST['question7']),
	mysql_real_escape_string($_POST['question8']),
	mysql_real_escape_string($_POST['question9']),
	mysql_real_escape_string($_POST['tandc']),  // This was out of sequence
	mysql_real_escape_string($_POST['comments'])   // I added this assuming you meant to

);

Link to comment
Share on other sites

Follow-up:

 

Keeping things straight in a query string that long can be confusing, as you have discovered.  Since your $_POST keys are the same as the column names into which they will be inserted, you could automate your query string generation so that it will remain fluid in the face of change.  You can change any of the values in the array below, and it will still generate a valid string (assuming you've used the right names).  I indented the query_string bit so that it's easier to decipher.

 

<?php
$data = array(
'fname',
'lname',
'position',
'email',
'state',
'areacode',
'prefix',
'linenumber',
'question1',
'question2',
'question2q1',
'question2q2',
'question2q3',
'question2q4',
'question2q5',
'question2q6',
'question3',
'question4q1',
'question4q2',
'question4q3',
'question4q4',
'question4q5',
'question5q1',
'question5q2',
'question5q3',
'question5q4',
'question5q5',
'question5q6',
'question6',
'question7',
'question8',
'question9',
'tandc',
'comments'
);

$query_string = 'INSERT INTO contact_n_questions ('
. implode(',',$data)
. ") VALUES ('"
. implode(
	"','",
	array_map(
		create_function(
			'$x',
			'global $_POST;return mysql_real_escape_string($_POST[$x]);'
		),
		$data
	)
)
. "')";

mysql_query($query_string);
?>

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.