<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="#" method="post">
UserName: <input type="text" name="username"><br />
Passward: <input type="text" name="passward"><br />
Name: <input type="text" name="name">
<input type="submit" name="Submit" value="Sent">
</form>


<?php 
$con=mysql_connect("localhost","root","","firstphp");

if(isset($_POST['Submit']))
 {
$UserName = $_POST['username'];
$Password = $_POST['passward'];    
$Name = $_POST['name'];


$sql="INSERT INTO users (username, passward, name)
VALUES ($UserName, $Password, $Name, NOW())";
$result = mysql_query($sql);

if (mysql_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

/*if($result){
echo "pass";
}
else {
echo "ERROR" . mysql_error($con);
}*/

mysql_close();
}
?>
</body>
</html>

Recommended Answers

All 5 Replies

my code give me an erroe when i click submit button.

it give me "Warning: mysql_query() expects parameter 1 to be string, resource given in F:\xampp\htdocs\phpprjct\Untitled-1.php on line 31"

and in other line my code shows "1 record added"

but when i refresh my database no record found.

Start by changing line 18 to:

$con=mysql_connect("localhost","root","","firstphp") or die(mysql_error());

and line 29 to:

$result = mysql_query($sql) or die(mysql_error);

You'll see that your query is missing single quotes around your string/varchar values (and perhaps password is misspelled).

Remove lines 31-33 because they execute the query again, and then use a mysqli error function, which can't work.

$UserName, $Password, $Name are always used as VARCHAR in database.
So you have to change your query to:

INSERT INTO users (username, passward, name) VALUES ('$UserName', '$Password', '$Name')

Moreover you select only 3 columns to insert the values but you were inserting 4 values. I removed NOW() from your query.

thank u for reply.

i tried both way but my code give me another error. "Use of undefined constant mysql_error - assumed 'mysql_error' in F:\xampp\htdocs\phpprjct\Untitled-1.php on line 28"

My bad, this

$result = mysql_query($sql) or die(mysql_error);

should be:

$result = mysql_query($sql) or die(mysql_error());

I forgot the parenthesis.

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.