Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'; 
}

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

INSERT INTO reps (name, `replace`, reg, boxinfo) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...