error in inserting records into MySQL

A

Anonymous

Guest
Hi,
i'm trying to write a page to add records into my MySQL database through a form,
but i cannot add record into it(i check it with the SELECT * from guestbook; command at the MySQL,but it return an empty set). Anyone know what's wrong with it?Thanks
here's the coding :

Code:
<?php 

$name=$HTTP_POST_VARS['name'];
$location=$HTTP_POST_VARS['location'];
$email=$HTTP_POST_VARS['email'];
$url=$HTTP_POST_VARS['url'];
$comments=$HTTP_POST_VARS['comments'];
$submit=$HTTP_POST_VARS['submit'];



$text="";
 	
 $db = @ mysql_pconnect('localhost', 'usr', 'pass');
 mysql_select_db('database');
 $checkQuery="select * from guestbook where email=\"$email\"";
 $result=mysql_query($checkQuery);
 $num_result=mysql_num_rows($result);


if (!$db)
{
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
}

mysql_select_db('B0300641');



if(empty($email))
{
  $text.="<li>Please enter email.</li>";
  $submit=false;
}
else if(!ereg("(.*@.*.\..*)",$email))
{
  $text.="<li>$email doesn't look like a valid e-mail address</li>";
  $submit=false;
}
else if($num_result!=0)
{
  $text.="<li>$email have already sign the guestbook.</li>";
  $submit=false;
}

if(empty($name))
{
  $text.="<li>Please enter name.</li>";
  $submit=false;
}

if(!empty($url) && !ereg("^http://.*.\..*",$url))
{
	$text.="<li>$url doesn't look like a valid URL</li>";
  	$submit=false;
}

 
  if ($submit)
  {
	$query="insert into guestbook(name,location,email,url,comments)
		values('".$name."','".$location."',
		'".$email."','".$url."',
		'".$comments."')";
	mysql_query($query);
	
	echo '<h2>Thanks!</h2>';
	echo '<h2><a href="view.php">View My Guest Book!!!</a></h2>';
   }
	else 
	{ 
		echo '<ul>';
		echo $text;
		echo '</ul>';
		include('sign.php');
	}
?>
 
One thing you could do to help you debug is enter the following before you assign your variables:

print('<pre>');
print_r($HTTP_POST_VARS);
print('</pre>');

this will print out your post array to make sure it contains data. Also, remove your @ sign before your connect call, and all other mysql function calls. Once the script works right, you could stick them back on.
 
Yeh ping98020, Skeletor is right. You can also use 'or die(mysql_error());' after your 'mysql_query();', so you can see if it´s everything alright with your query!

BTW: Try to use (i suggest you):
Code:
if (isset($_POST['submit']))
Instead of:
Code:
if ($submit)
Maybe you can have your register_globals off, so it will not make that if statement!
 
Back
Top