Hi,

I have created table in database mysql and I created register form and I wrote insert code ,when we run the php file there is no but given data is not inserted in database....I need help,I am new to php....


This is my code i have used,........

<?php
if(isset($_POST)){
$host= "localhost";
$username= "root";
$password= "";
$db_name= "register";
$tbl_name="form";

mysql_connect("$host","$username","$password") ;
mysql_select_db("$register") ;

$fname= $_POST;
$lname =$_POST;
$eid =$_POST;
$pwd=$_POST;
$cpwd=$_POST;

$sql="INSERT INTO $form (id , fname ,lname, eid , pwd,cpwd) VALUES('NULL',' $fname','$lname','$eid','$pwd','$cpwd')";
$result=mysql_query($sql);

// close connection
mysql_close();
}

?>

Dani AI

Generated

The posted script shows several classic mistakes that commonly stop INSERTs: wrong variable names for the database and table, assigning $_POST without field keys, quoting an auto‑increment id incorrectly, and relying on the old mysql_* extension (which is removed from modern PHP). correctly called out the undefined $form; was right to push for turning on error reporting; 's suggestion to use a blank string instead of NULL is not appropriate — either omit an AUTO_INCREMENT column from the INSERT or use SQL NULL (unquoted) if explicitly needed.

A compact troubleshooting checklist that addresses the thread's issues:

  • Verify the HTML form uses method="post" and that every input has the correct name attribute (match those keys when reading $_POST).
  • Assign posted values with their keys (for example, read $_POST['fname']), validate and trim them, and never store plaintext passwords — use password_hash.
  • Omit the auto_increment id column in the column list or use SQL NULL (without quotes) rather than a quoted string.
  • Replace ext/mysql with PDO or mysqli and use prepared statements to avoid SQL injection.

Example (PDO + hashed password):

$pdo = new PDO('mysql:host=localhost;dbname=register','root','');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('INSERT INTO form (fname,lname,eid,pwd) VALUES (:f,:l,:e,:p)');
$stmt->execute([':f'=>$fname,':l'=>$lname,':e'=>$eid,':p'=>password_hash($pwd,PASSWORD_DEFAULT)]);

Enable exceptions/error reporting while debugging and confirm the actual database and table names exist. See PDO prepared statements and password_hash for best practices.

Recommended Answers

All 3 Replies

if id is primarykey, replace NULL with ' ' and try once.

use die(mysql_error()) to see whats wrong into your query.

<?php
if(isset($_POST['submit'])){
$host= "localhost";
$username= "root";
$password= "";
$db_name= "register";
$tbl_name="form";

mysql_connect("$host","$username","$password") or die(mysql_error());;
mysql_select_db("$register") or die(mysql_error());;

$fname= $_POST['fname'];
$lname =$_POST['lname'];
$eid =$_POST['eid'];
$pwd=$_POST['pwd'];
$cpwd=$_POST['cpwd'];

$sql="INSERT INTO $form (id , fname ,lname, eid , pwd,cpwd) VALUES('NULL',' $fname','$lname','$eid','$pwd','$cpwd')";
$result=mysql_query($sql) or die(mysql_error());

// close connection
mysql_close();
}

?>

What is $form in insert query?
Bcz it doesn't defined in above code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.