Php 无法使用insert INTO将数据插入mysql表

Php 无法使用insert INTO将数据插入mysql表,php,mysql,html-table,wamp,Php,Mysql,Html Table,Wamp,在WAMP和phpMyadmin中,我用这个表创建了一个名为PROJECT的数据库 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_IN

在WAMP和phpMyadmin中,我用这个表创建了一个名为PROJECT的数据库

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 ;
我正在尝试测试以下代码:

<?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>
$s=“插入用户值(“,$a.”,“,$b.”)

你必须插入。在查询中,以使其工作。像这样试试。

试试这个

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

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

首先,您应该尝试一种更安全的方法,以确保只插入要插入的数据。例如:

$test=mysql_query("insert into user(username,password) values('$a','$b')");
然后检查是否有任何错误

if(!$test || mysql_error())
     echo "Error";
else
     echo "Inserted";
注意:注意,id不会出现,因为它是自动递增的,并且由于它是自动递增的,所以您不应该插入值,否则您将面临错误


编辑:我忘了提到,如果你喜欢(
值(“,$a”,“$b”)
),这实际上是不必要的,目前还不确定,但我认为它确实给出了一个错误,因为你在字段中插入了“==null==0”。

嗯,你是舒尔吗?我不认为so@steven我刚刚编辑了我的答案,添加了“在“引号”后面加引号。用这个替换你现有的状态并检查它。我不知道,这不是我的问题,但我认为@echo_Me更接近修复。没有点的版本和你的几乎一样。如果一个版本有效,另一个版本也应该有效,因为外部的双引号。据我所知,在php中,变量应该在注释之外。因此,您应该使用“.$A.”规则这是否回答了您的问题?
if(!$test || mysql_error())
     echo "Error";
else
     echo "Inserted";