-1

I am trying to figure out why an error occurs when my PHP script tries to insert data into a MySQL database.

I am working on a small form that collects data, and then in PHP I submit it into my SQL database. When I submit it, I get a blank page and nothing is inserted, I have added an echo to display if the insert fails, which it is.

<?php
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$name=$_POST['repname'];
$replace=$_POST['replace'];
$reg=$_POST['reg'];
info=$_POST['info'];

if($name == '') {
    $errmsg_arr[] = 'Rep name missing.';
    $errflag = true;
}

if($replace == '') {
$errmsg_arr[] = 'Please fill out all the forms.';
$errflag = true;
}
if($reg == '') {
$errmsg_arr[] = 'Please fill out all the forms.';
$errflag = true;
}
if($info == '') {
$errmsg_arr[] = 'Please add some other information about your rep.';
$errflag = true;
}


if(!mysql_query("INSERT INTO reps (name, replace, reg, boxinfo) VALUES          
('".$name."','".$replace."','".$reg."','".$info."')"))   
{ 
 echo 'failed'; 
} 
else 
{ 
 echo 'inserted'; 
}

?>
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
codsane
  • 68
  • 9

1 Answers1

3

Beside Shankar's comment replace is a reserved word use backticks.

INSERT INTO reps (name, `replace`, reg, boxinfo) 
Community
  • 1
  • 1
Mihai
  • 26,325
  • 7
  • 66
  • 81