alvinchua Posted May 2, 2009 Share Posted May 2, 2009 Hello, I have a problem where my script always giving me the error 'Query Failed'. Can anyone help me to figure out what is the problem with my code ? Thanks in advanced <?php if(!require('connection.php')){ echo "Error in page process!"; } $name=$_POST['name']; $country=$_POST['country']; $address=$_POST['address']; $email=$_POST['email']; $inquiry_type=$_POST['inquiry_type']; $message=$_POST['message']; $date=$_POST['date']; $sql = "INSERT INTO inquiry VALUES(NULL,'".$name."','".$country."','".$email."','".$address."','".$inquiry_type."','".$message."','".$date."');"; $result = mysql_query($sql) or die ("Query failed!"); $to = "[email protected]"; $headers = "From:$email"; $sent = mail($to, "New inquiry", "Inquiry type:$inquiry_type\nPlease go to admin page to check the new inquiry.\n http://www.2ndhome-intl.com/admin/", $headers) ; if(!$sent) {print "We encountered an error sending your inquiry, please try again!"; } else {?> <script language="javascript" type="text/javascript"> alert("Your inquiry has been submitted, Thank you"); window.location = "index.php"; </script> <?php }?> Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 Why not get php/mysql to tell you why it failed. Add a mysql_error() statement to your die() statement - $result = mysql_query($sql) or die ("Query failed! " . mysql_error()); P.S. A require() statement that fails is a fatal runtime error. Code execution stops at the require() statement and your echo "Error in page process!"; will never be executed if the require() fails. Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-823994 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Possible case: <?php if(!require('connection.php')){ echo "Error in page process!"; } $name=mysql_real_escape_string($_POST['name']); $country=mysql_real_escape_string($_POST['country']); $address=mysql_real_escape_string($_POST['address']); $email=mysql_real_escape_string($_POST['email']); $inquiry_type=mysql_real_escape_string($_POST['inquiry_type']); $message=mysql_real_escape_string($_POST['message']); $date=mysql_real_escape_string($_POST['date']); $sql = "INSERT INTO inquiry VALUES(NULL,'".$name."','".$country."','".$email."','".$address."','".$inquiry_type."','".$message."','".$date."');"; $result = mysql_query($sql) or die ("Query failed!"); $to = "[email protected]"; $headers = "From:$email"; $sent = mail($to, "New inquiry", "Inquiry type:$inquiry_type\nPlease go to admin page to check the new inquiry.\n http://www.2ndhome-intl.com/admin/", $headers) ; if(!$sent) {print "We encountered an error sending your inquiry, please try again!"; } else {?> <script language="javascript" type="text/javascript"> alert("Your inquiry has been submitted, Thank you"); window.location = "index.php"; </script> <?php }?> Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-823997 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 Posting "fixed" code without any explanation of what it corrects or does differently, teaches nothing. It's also possible that while the external data does need to be escaped, that the actual cause of the failed query is due to something else. Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-824002 Share on other sites More sharing options...
radomTraveler Posted May 2, 2009 Share Posted May 2, 2009 Are you cenrtain that the table exists? And that all fields exist, and that there are no typos? Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-824029 Share on other sites More sharing options...
alvinchua Posted May 2, 2009 Author Share Posted May 2, 2009 hmm i tried the code posted here but it give me the same error. i have include this line into my code $result = mysql_query($sql) or die ("Query failed! " . mysql_error()); and it give me the error as below Query failed! Duplicate entry '127' for key 1 anyone ? Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-824031 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 Your table definition has a TINYINT for one of the indexes and you have used all the available values. You should use at least a normal INT for an index. Alter your table and change the column that is a TINYINT into an INT data type. Link to comment https://forums.phpfreaks.com/topic/156480-query-failed/#findComment-824043 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.