帮我解决这个代码我是php的新手
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$sql="INSERT INTO registration(fname,designation,emailid,
address,phonenumber)VALUES('".$_POST['fname']."','".$_POST['designation']."','".$_POST['ema
lid']."', '".$_POST['address']."','".$_POST['phonenumber']."')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
它的注册页面获取值并将其插入表格中......
答案 0 :(得分:1)
这里的参数错误:
$result=mysql_query($conn,$sql);
尝试
$result=mysql_query($sql, $conn) or die(mysql_error($conn));
附注:
mysql_*()
函数:它们已被弃用。请改用mysqli_*()
版本。mysql_real_escape_string()
转义用户输入以防止SQL注入攻击。请考虑使用mysqli_()
代替预备语句。答案 1 :(得分:0)
答案 2 :(得分:0)
如果你想拥有可读的代码,将$_POST[]
值设置为变量,然后将它们传递给查询,事实上它并没有什么不同,但这更简单,更干净。:
<?php
$conn=mysql_connect("localhost","root","","test");
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$designation = $_POST['designation'];
$emailid = $_POST['emailid'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
$sql="INSERT INTO registration(fname,designation,emailid,address,phonenumber)";
$sql .="VALUES('$fname', '$designation', '$emailid', '$address', '$phonenumber')";
echo $sql;
$result=mysql_query($conn,$sql);
echo $result;
}
else{
echo "Error";
}
?>
你在$_POST['emailid']
...中犯了打字错误
你可以用这个选择你的数据库:
mysql_select_db('your db name');
在您的连接变量意味着$conn
这是错误的:
$result = mysql_query ($conn, $sql)
您必须先设置查询:
$result = mysql_query($sql, $conn)