Jump to content

Help on this code please.


Saydfx
Go to solution Solved by Saydfx,

Recommended Posts

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'

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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')";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.