Page 1 of 1

Need Help Adding a Record to MySQL using PHP

Posted: Tue Oct 03, 2006 6:49 am
by jawinn
twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I've been to a ton of different forums and can't get a straight answer on my seemingly simple problem.  I am a total n00b with PHP and MySQL so please excuse my ignorance.  I want to be able to have a form (which I already have) dump the submitted info into a MySQL db.  I was able to create the table in the DB; but I can't get any info into it.

Here is the code I'm using now:

form:
[syntax="html"]
<form method="post" action="answer.php" />
First Name&nbsp;&nbsp;&nbsp;Last Name<br> 
<input type="text" name="fname" /><input type="text" name="lname" /><br/><br>
City&nbsp;&nbsp;&nbsp;State<br>
<input type="text" name="city" /><select name="state" />
	<option>Alabama</option>
		<option>Wyoming</option>
	</select><br/><br>
	E-mail Address<br>
	<input type="text" name="email" /><br/><br>
Answer: <input type="radio" name="answer" value="A" /> A<input type="radio" name="answer" value="B" /> B<input type="radio" name="answer" value="C" /> C<input type="radio" name="answer" value="D" /> D<br/>
<input type="checkbox" name="remember" value="1" />Remember Me<br/><br/>
<input type="submit" name="submit" value="Submit" />
</form> 
answer.php[/syntax]

Code: Select all

<?php

$handle = mysql_connect('dbserver.com', 'dbname','password');

echo ($handle) ? "Connected to MySQL.\r\n" : "Could not connect to MySQL.\r\n";

mysql_select_db('test1002') or die('Cannot select database'); 

    $error = false;

if(isset($_POST['submit'])); { 

$form = array();
$form['fname'] = $_POST['fname'];
$form['lname'] = $_POST['lname'];
$form['email'] = $_POST['email'];
$form['city'] = $_POST['city'];
$form['state'] = $_POST['state'];
$answer = $_POST['answer']; 

if(!ini_get('magic_quotes_gpc')); {

foreach($form as $key => $value); {
$form[$key] = mysql_escape_string($value);
}
}

$query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('{$form['fname']}', '{$form['lname']}', '{$form['email']}', '{$form['city']}','{$form['state']}', '{$form['answer']}',)"; 

$result = $database->query($query);

?>
The error I get when I submit the form is:

Parse error: parse error, unexpected $ in xxx/xxx/xxx/xxx/answer.php on line 33

Any help is much appreciated.

Thanks in advance,
jawinn


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Oct 03, 2006 7:01 am
by volka
if(isset($_POST['submit'])); {
[...]
if(!ini_get('magic_quotes_gpc')); {
no ; after if()
I would be an empty statement, meaning "if (condition) do nothing".


If you indent your code carefully you will find that there's a } missing.

Code: Select all

<?php
$handle = mysql_connect('dbserver.com', 'dbname','password');
echo ($handle) ? "Connected to MySQL.\r\n" : "Could not connect to MySQL.\r\n";
mysql_select_db('test1002') or die('Cannot select database');

if(isset($_POST['submit'])) {
	$form = array();
	if (ini_get('magic_quotes_gpc')) {
		$_POST = array_map('stripslashes', $_POST); // magic_quote/addslashes is insuffcient.
	}
	foreach( array('fname', 'lname', 'email', 'city', 'state', 'answer') as $key) {
		$form[$key] = mysql_real_escape_string($_POST[$key], $handle);
	}

	$query = "INSERT INTO
			centries
			(fname,lname,email,city,state,answer)
		VALUES
			('{$form['fname']}', '{$form['lname']}', '{$form['email']}',
			 '{$form['city']}','{$form['state']}', '{$form['answer']}',)";
	$result = $database->query($query);
}
?>

Posted: Tue Oct 03, 2006 7:08 am
by jawinn
Thanks! I'll give this a try.

If you indent your code carefully you will find that there's a } missing.

Where?

Posted: Tue Oct 03, 2006 7:11 am
by volka
jawinn wrote:If you indent your code carefully you will find that there's a } missing.

Where?
Format the code properly and you will see. Just do it.

Posted: Tue Oct 03, 2006 7:14 am
by jawinn
Ok, tried the new code and now I get this:

Connected to MySQL.
Fatal error: Call to a member function on a non-object in xxx/xxx/xxx/answer.php on line 21

Posted: Tue Oct 03, 2006 7:16 am
by volka
line 21 probably is
$result = $database->query($query);
(please always mark the line of code that is mentioned in the error message)

What is $database? I only see
$handle = mysql_connect('dbserver.com', 'dbname','password');

Posted: Tue Oct 03, 2006 7:20 am
by jawinn
volka wrote:line 21 probably is
$result = $database->query($query);
(please always mark the line of code that is mentioned in the error message)
Will do, sorry.
volka wrote:line 21 probably is
What is $database? I only see
$handle = mysql_connect('dbserver.com', 'dbname','password');
The database name is test1002. I didn't include it b/c I don't know what I should and should not include for the sake of security. Sorry for the confusion.

thx

Posted: Tue Oct 03, 2006 7:23 am
by volka
There is no variable $database in that script up to
$result = $database->query($query);
What is $database? It is used as an object ($object->method()) but it doesn't seem to be declared/assigned/whatever.

Posted: Tue Oct 03, 2006 7:27 am
by jawinn
Good point. I never noticed that $database was undefined. Do I need that line of code at all? I've changed this script so many times trying to get it right that I don't recall now why it was there in the first place.

Posted: Tue Oct 03, 2006 7:29 am
by volka
try

Code: Select all

$result = mysql_query($query, $handle);
if ( false===$result ) {
	echo 'error: ', mysql_error();
}
else {
	echo mysql_affected_rows($handle), ' records have been inserted';
}

Posted: Tue Oct 03, 2006 7:32 am
by jawinn
Now I have:

<?php
$handle = mysql_connect('server.net', 'test1002','password');
echo ($handle) ? "Connected to MySQL.\r\n" : "Could not connect to MySQL.\r\n";
mysql_select_db('test1002') or die('Cannot select database');

if(isset($_POST['submit'])) {
$form = array();
if (ini_get('magic_quotes_gpc')) {
$_POST = array_map('stripslashes', $_POST); // magic_quote/addslashes is insuffcient.
}
foreach( array('fname', 'lname', 'email', 'city', 'state', 'answer') as $key) {
$form[$key] = mysql_real_escape_string($_POST[$key], $handle);
}

$query = "INSERT INTO
centries
(fname,lname,email,city,state,answer)
VALUES
('{$form['fname']}', '{$form['lname']}', '{$form['email']}',
'{$form['city']}','{$form['state']}', '{$form['answer']}',)";
$result = mysql_query($query, $handle);
if ( false===$result ) {
echo 'error: ', mysql_error();
}
else {
echo mysql_affected_rows($handle), ' records have been inserted';
}
?>

The error I get is:
Parse error: parse error, unexpected $ in xxx/xxx/xxx/answer.php on line 28

Line 28 is (if I'm correct in assuming that PHP code starts with 1 and not 0):
?>

Posted: Tue Oct 03, 2006 7:40 am
by volka
Again missing a closing }

Posted: Tue Oct 03, 2006 7:42 am
by jawinn
I just added a } at the very end and now I get a new error:

Connected to MySQL. 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 6

if(isset($_POST['submit'])) {

Posted: Tue Oct 03, 2006 7:44 am
by volka
This is a mysql error. Therefore the line number refers to the mysql query.
INSERT INTO
centries
(fname,lname,email,city,state,answer)
VALUES
('{$form['fname']}', '{$form['lname']}', '{$form['email']}',
'{$form['city']}','{$form['state']}', '{$form['answer']}',)
remove the comma

Posted: Tue Oct 03, 2006 7:49 am
by jawinn
YEEEEEEEEEEESSSSSSSSSSSSS!!!!!!!!! :D :D :D :D :D :D :D

You Rock!!!

This has been killing me for two days. I swore to myself that I would find an answer by noon today Est. With your help I did. Thank you so much for your help. I will now make this my "go to" PHP community.

Thank you,
jawinn