1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dali1985
  • 3,263
  • 13
  • 49
  • 68
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Feb 29 '20 at 15:37

4 Answers4

3

try this

    if(isset($_POST['inser']))
    { 
    $s="INSERT INTO users (`username` , `password` )VALUES ('$a','$b')";
       mysql_query($s);
    echo "Your Data Inserted";
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

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

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

Ideal Bakija
  • 629
  • 5
  • 14
  • ehm, are you shure? i dont think so – steven Aug 06 '13 at 19:40
  • @steven I just edited my answer adding the " quotes after the ' quotes. Replace your existing your existing statment with this one and check it. – Ideal Bakija Aug 06 '13 at 19:43
  • i dont know, it is not my question but i think @echo_Me is closer to the fix. The Version without the dots is the nearly the same as yours. if one version works the other should work, too because of the outer double quotes. – steven Aug 06 '13 at 19:48
  • As far as I know in php variable should be outside of the comment. Therefore you should use the ' " . $ A . " ' rule – Ideal Bakija Aug 06 '13 at 19:51
0
$s="INSERT INTO users VALUES ('','$a','$b')";

should be something like:

$s="INSERT INTO users VALUES ('$a','$b')";
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
0

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.

calexandru
  • 307
  • 1
  • 3
  • 16