dali1985
dali1985

Reputation: 3323

Cannot insert data to mysql table using INSERT INTO

In WAMP and in phpMyadmin I created a database called PROJECT with this table

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

I am trying to test the following code:

<?php
$con=mysql_connect ("localhost","******","");
mysql_select_db("project",$con);
@$a=$_POST['txt1'];
@$b=$_POST['txt2'];
if(@$_POST['inser'])
{
 $s="INSERT INTO users VALUES ('','$a','$b')";
echo "Your Data Inserted";
 mysql_query ($s);
}
$con=mysql_connect ("localhost","******","");
mysql_select_db ("project",$con);
if(@$_POST ['sel'])
{
echo $ins=mysql_query ("select * from users");
echo "<table bgcolor=skyblue border='2'>
<tr>
<th colspan=4>Display details</th></tr>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>";
while ($row=mysql_fetch_array ($ins))
{
echo "<tr>";
echo  "<th>".$row ['id']."</th>";
echo  "<th>".$row ['username']."</th>";
echo  "<th>". $row ['password']. "</th>";

echo "</tr>";
}
}
echo "</table>"
?>
<html>
<head>
</head>
<body bgcolor="pink">
<table bgcolor="skyblue" border="2">
<form method="post">
<tr>
<td colspan=2 align="center">Details</td>
</tr>
<td>Username</td><td><input type="text" name="txt1"></td>
</tr>
<tr>
<td>Password</td><td><input type="text" name="txt2"></td>
</tr>
<tr>
<td><input type="submit" name="inser" value="Insert"></td>
<td><input type="submit" name="sel" value="Select"></td>
</tr>
</form>
</table>
</body>
</html>

When I insert manually username and password in the database and click the Select button then I have the record. When I try to insert a record using the INSERT INTO above it is impossible to add the record. I have the message YOUR DATA INSERTED but there is no record in the database. I tried it without the field ID and it works perfect. I tried it also without AUTO INCREMENT but without results. So what is the problem with the ID?

Upvotes: 1

Views: 3213

Answers (4)

calexandru
calexandru

Reputation: 307

First of all, you should try a safer method, just to make sure you insert ONLY the data you want to be inserted. For example:

$test=mysql_query("insert into user(username,password) values('$a','$b')");

then check if you got any errors

if(!$test || mysql_error())
     echo "Error";
else
     echo "Inserted";

Note: note that the id doesn't appear because it is auto-increment, and since is auto-incremented you don't and shouldn't insert values otherwise you will face an error.

EDIT: I forgot to mention that if you do like you did (values('','$a','$b')) it's actually unnecessary and not sure at the moment but I think it does give an error since you are inserting ''==null=='0' into the field.

Upvotes: 0

Deepak Biswal
Deepak Biswal

Reputation: 4320

$s="INSERT INTO users VALUES ('','$a','$b')";

should be something like:

$s="INSERT INTO users VALUES ('$a','$b')";

Upvotes: 0

echo_Me
echo_Me

Reputation: 37253

try this

    if(isset($_POST['inser']))
    { 
    $s="INSERT INTO users (`username` , `password` )VALUES ('$a','$b')";
       mysql_query($s);
    echo "Your Data Inserted";

Upvotes: 3

Ideal Bakija
Ideal Bakija

Reputation: 639

$s="INSERT INTO users VALUES ('','".$a."','".$b."')";

You have to insert . in the query for it to work. Try it like this.

Upvotes: 0

Related Questions