Not inserting to database

<?php
include "config.php";

?>
<center><H3>Register for the LAN Event!</H3></center>
<form method="post" action="thankyou.php">

	<div align="center"><center><table border="1" width="90%">
		<tr>
			<th width="30%" nowrap>First Name</th>
			<td width="70%"><input type="text" name="firstname" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Last Name</th>
			<td width="70%"><input type="text" name="lastname" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Birthdate</th>
			<td width="70%"><input type="text" name="birthdate" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Email</th>
			<td width="70%"><input type="text" name="email" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Phone</th>
			<td width="70%"><input type="text" name="phone" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap><input type="submit" name="submit" value="Submit" />
			<input type="reset" value="Reset" /></th>
		</tr>
	</table>
	</center>
	</div>
</form>
	
<?
// registration checks


if($_POST['submit']){
 
$errors = ""; 
if (!isset($_POST['firstname'])) 
    $errors .= "Please provide a first name. <br />"; 
if (!isset($_POST['lastname'])) 
    $errors .= "Please provide a last name. <br />"; 
if (!isset($_POST['birthdate'])) 
    $errors .= "Please provide a birthdate. <br />"; 
if (!isset($_POST['email'])) 
    $errors .= "Please provide a email address. <br />"; 
if (!isset($_POST['phone'])) 
    $errors .= "Please provide a phone number. <br />"; 
if ($errors == "") { 


// query

	$query = "INSERT INTO `attendees` (`firstname`,`lastname`,`birthdate`,`email`,`phone`) VALUES('".addslashes($_POST['firstname']) . "','" .addslashes($_POST['lastname']). "','" .addslashes($_POST['birthdate']). "','" .addslashes($_POST['email']). "','" .addslashes($_POST['phone']). "')";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());
}
}

?>	

I do not see any errors, but it is not inserting into the database. Can anyone tell me whats wrong?

Yes.

Yes, I have wamp server running. and both Apache and MySQL are running.

Is this the code of the ‘thankyou.php’ script?

How do you use mysql_real_escape_string?

and I took out the backticks.

Like addslashes. Just substitute addslashes with mysql_real_escape_string. But this won’t resolve your problem, it’s just something to improve the script :slight_smile:

Please answer 2) and try to do 3) and let us know the results. Ah, and do an echo of $query please.

try taking out your backticks in your query

  1. don’t use addslashes, instead use mysql_real_escape_string
  2. what does error_message() do? and where did you define that function?
  3. you could add some echo’s in ‘thankyou.php’ to see what is executed and what not, and do a print_r of $_POST at the beginning of the script to check its values.

thankyou.php

<?
include "config.php";

if($_POST['submit']){
 
$errors = ""; 
if (!isset($_POST['firstname'])) 
    $errors .= "Please provide a first name. <br />"; 
if (!isset($_POST['lastname'])) 
    $errors .= "Please provide a last name. <br />"; 
if (!isset($_POST['birthdate'])) 
    $errors .= "Please provide a birthdate. <br />"; 
if (!isset($_POST['email'])) 
    $errors .= "Please provide a email address. <br />"; 
if (!isset($_POST['phone'])) 
    $errors .= "Please provide a phone number. <br />"; 
if ($errors == "") { 


// query

	$query = "INSERT INTO `attendees` (`firstname`,`lastname`,`birthdate`,`email`,`phone`) VALUES('".addslashes($_POST['firstname']) . "','" .addslashes($_POST['lastname']). "','" .addslashes($_POST['birthdate']). "','" .addslashes($_POST['email']). "','" .addslashes($_POST['phone']). "')";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());
}
}

	
?>

Ok, then please post the entire ‘thankyou.php’ script.

nevermind… let me look at it.

Did you put the include to config.php in ‘thankyou.php’?

Yes I did.

This is now my thankyou.php file:

<?
// registration checks


if($_POST['submit']){
 
$errors = ""; 
if (!isset($_POST['firstname'])) 
    $errors .= "Please provide a first name. <br />"; 
if (!isset($_POST['lastname'])) 
    $errors .= "Please provide a last name. <br />"; 
if (!isset($_POST['birthdate'])) 
    $errors .= "Please provide a birthdate. <br />"; 
if (!isset($_POST['email'])) 
    $errors .= "Please provide a email address. <br />"; 
if (!isset($_POST['phone'])) 
    $errors .= "Please provide a phone number. <br />"; 
if ($errors == "") { 


// query

	$query = "INSERT INTO `attendees` (`firstname`,`lastname`,`birthdate`,`email`,`phone`) VALUES('".addslashes($_POST['firstname']) . "','" .addslashes($_POST['lastname']). "','" .addslashes($_POST['birthdate']). "','" .addslashes($_POST['email']). "','" .addslashes($_POST['phone']). "')";
	$result = mysql_query($query);
	if(!$result) error_message(sql_error());
}
}

	
?>

And this is register.php:

<center><H3>Register for the LAN Event!</H3></center>
<form method="post" action="thankyou.php">

	<div align="center"><center><table border="1" width="90%">
		<tr>
			<th width="30%" nowrap>First Name</th>
			<td width="70%"><input type="text" name="firstname" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Last Name</th>
			<td width="70%"><input type="text" name="lastname" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Birthdate</th>
			<td width="70%"><input type="text" name="birthdate" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Email</th>
			<td width="70%"><input type="text" name="email" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap>Phone</th>
			<td width="70%"><input type="text" name="phone" size="15" /></td>
		</tr>
		<tr>
			<th width="30%" nowrap><input type="submit" name="submit" value="Submit" />
			<input type="reset" value="Reset" /></th>
		</tr>
	</table>
	</center>
	</div>
</form>
	

and this is config.php:


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

?>

it just doesnt work. it wont connect i dont think. or my insert query is wrong. do you see any errors?

Try echoing $query, does anything look out of place? if you can’t see anything that looks out of place, try the query via phpmyadmin

Then you’ll have to move the PHP code at the end of the form to the top of ‘thankyou.php’. Or you’ll have to have the form call the ‘register.php’ script, move the PHP code to the top of the script, and in case of successful registration, redirect to ‘thankyou.php’

The point is, that the entire ‘register.php’ script is executed at once, server side, and then the resulting HTML page (the form) is sent to the browser. Once the user clicks the send button, the script DOES NOT continue at the end of the form. The script called by the form (in this case thankyou.php) is executed (on the server) from the beginning to the end, and the resulting HTML page is sent to the browser.
So, in your case, the part of the script that does the insert is never executed. The first time it isn’t executed because the form hasn’t been submitted yet, and after form submition it isn’t executed because the script that contains the insert code isn’t called.

No; this is register.php

I see you have localhost listed… are you using xampp or something? if so is apache and mysql running?

The text that I put at the top of the page before the <?php tag. It showed up.

So, right now, the code looks like:


asdfjadfj
<?php
include "config.php";
print_r ($_POST);
?>

…and? Did you suceed?

mysql_real_escape_string