Saydfx Posted July 7, 2014 Share Posted July 7, 2014 This is form processing page, i create but it fail to add new record without any error message, please tell me where i do wrong. <?php $id=$_POST['ID']; $name=$_POST['name']; $address=$_POST['address']; $contact=$_POST['contact']; $coment=$_POST['comment']; include("inc/db_connect.php"); $pagetitle="Add new Customer"; include("inc/header.php"); ?> <div class="content"> <?php $sql="INSERT INTO db(ID, Name, Address, Contact, Comment) VALUES($id, $name, $address, $contact, $coment"; if(mysqli_query($con,$sql)){ echo "<h2>".$name." added!</h2>"; }else { echo "<h2>".$name." nOT Added!</h2>"; } ?> It always go to 'NOT ADDED' Quote Link to comment https://forums.phpfreaks.com/topic/289515-help-on-this-code-please/ Share on other sites More sharing options...
mac_gyver Posted July 7, 2014 Share Posted July 7, 2014 the message you are getting comes from the logic branch for when the query has failed due to an error. you can echo mysqli_error($con) as part of that logic to find out why the query has failed, assuming that $con actually is a valid database connection. if $con doesn't actually contain a valid database connection, you would be getting a php error message. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would be reporting and displaying all the errors it detects? Quote Link to comment https://forums.phpfreaks.com/topic/289515-help-on-this-code-please/#findComment-1484098 Share on other sites More sharing options...
maxxd Posted July 7, 2014 Share Posted July 7, 2014 Couple things to add to mac_gyver's answer. First, ID in a table is typically (not always, so I'm making an assumption) an auto-increment field, which means you won't insert a value into that field at all. Drop it from the column and values lists. It also looks like you're missing the closing parenthesis in the values list of your query. Finally, strings need to be quoted, so $coment would be '$coment', etc. Turn on error reporting - it'll help. Quote Link to comment https://forums.phpfreaks.com/topic/289515-help-on-this-code-please/#findComment-1484109 Share on other sites More sharing options...
Zane Posted July 7, 2014 Share Posted July 7, 2014 Encase your variables with single quotes, especially for those fields whose datatype is of the string family. ALSO, you are missing the end parenthesis for VALUES. $sql = "INSERT INTO db (ID, Name, Address, Contact, Comment) VALUES('$id', '$name', '$address', '$contact', '$coment')"; Quote Link to comment https://forums.phpfreaks.com/topic/289515-help-on-this-code-please/#findComment-1484141 Share on other sites More sharing options...
Solution Saydfx Posted July 11, 2014 Author Solution Share Posted July 11, 2014 Thanks guys....it worked. Quote Link to comment https://forums.phpfreaks.com/topic/289515-help-on-this-code-please/#findComment-1484666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.