php - 向 mysql 数据库添加数据

标签 php mysql forms

我正在尝试制作一个表单来将数据添加到我的数据库中,但出现错误

“无法输入数据:您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在 ') 附近使用的正确语法 VALUES ('s','d','f' ,'g','h')' 在第 1 行"

这是我使用的代码

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
   $make = addslashes ($_POST['make']);
   $model = addslashes ($_POST['model']);
   $value = addslashes ($_POST['value']);
   $bcosts = addslashes ($_POST['bcosts']);
}
else
{
   $make = $_POST['make'];
   $model = $_POST['model'];
   $value = $_POST['value'];
   $bcosts = $_POST['bcosts'];
}
$rcosts = $_POST['rcosts'];

$sql="INSERT INTO referb (make,model,rcosts,bcosts,value,) VALUES ('$make','$model','$rcosts','$bcosts','$value')";  

mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">make</td>
<td><input name="make" type="text" id="make"></td>
</tr>
<tr>
<td width="100">model</td>
<td><input name="model" type="text" id="model"></td>
</tr>
<tr>
<td width="100">repair costs</td>
<td><input name="rcosts" type="text" id="rcosts"></td>
</tr>
<tr>
<td width="100">paid costs</td>
<td><input name="bcosts" type="text" id="bcosts"></td>
</tr>
<tr>
<td width="100">value</td>
<td><input name="value" type="text" id="value"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

我是个新手,所以我问这个问题,哈哈,别担心,这永远不会是一个实时网站:)

最佳答案

value, 替换为 value:

$sql="INSERT INTO referb (make,model,rcosts,bcosts,value,) VALUES ('$make','$model','$rcosts','$bcosts','$value')";  

所以就变成了:

$sql="INSERT INTO referb (make,model,rcosts,bcosts,value) VALUES ('$make','$model','$rcosts','$bcosts','$value')";  

关于php - 向 mysql 数据库添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284140/

相关文章:

php - 如何为特定酒店添加房间类别

python - flask 类型错误 : 'ImmutableMultiDict' object is not callable

java - 如何解决 Jenkins build error on recording test results?

php - MySQL 快速批量插入

php - 将 Composer 存储库集成到 php 文件中

php - 按 ID 或名称过滤结果

phpmyadmin - 导出 MySQL 数据库

php - fileatime() "access"是文件吗?

model-view-controller - spring 3.0 MVC 似乎忽略了 messages.properties

javascript - 表单提交重定向无法使用 Contact Form 7 WordPress 插件