php 和 sql 表单的问题
好的,我是 php 和 sql 新手,我有一个将一些名称和城市提交到数据库的表单。
我设法做到了,但是一旦点击提交按钮,我收到一个错误:
“错误:您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解要使用的正确语法在第 1 行的“1”附近”
但是,当我检查 phpmyadmin 时,新记录就在那里!!,所以我不确定出了什么问题,这就是问题所在。
这是代码:
<?php
mysql_connect("localhost", "name", "pass") or die(mysql_error());
echo "Connection to the server was successful!<br/>";
mysql_select_db("db_name") or die(mysql_error());
echo "Database was selected!<br/>";
$resultComuna = mysql_query("SELECT idComuna, nombre FROM comuna ORDER BY nombre ASC");
$resultGiro = mysql_query("SELECT idGiro, nombre FROM giro ORDER BY nombre ASC");
?>
<html>
<head>
<title>TEST</title>
</head>
<body>
<br/><br/>
<form name="form" method="POST" action="test_action.php">
<div align="center">
<!--///////////////// input nombre //////////////////////// -->
NOMBRE CLIENTE:
<input name="nombreCliente" type="text" maxlength="30" size="40"></>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////////////drop box para giro ///////////////////// -->
GIRO:
<select name="giro">
<?php
while($row = mysql_fetch_assoc($resultGiro)){
echo "<option value=\"".$row['idGiro']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////// dropbox para comunas //////////////////////// -->
COMUNA:
<select name="comunas">
<?php
while($row = mysql_fetch_assoc($resultComuna)){
echo "<option value=\"".$row['idComuna']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ////////////////////////////////////////////////////////////// -->
<input type="submit" value="Ingresar"> </>
</div>
</form>
</body>
</html>
test_action.php 是:
<?php
$con = mysql_connect("localhost", "name", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data_base", $con);
$query = mysql_query("SELECT max(idNombre)+1 as id FROM nombre");
$row = mysql_fetch_array($query);
$idMax = $row['id'];
$sql = mysql_query("INSERT INTO nombre VALUES ('".$idMax."','".$_POST['comunas']."',".$_POST['giro'].",'".$_POST['nombreCliente']."')");
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
mysql_close($con)
?>
Ok, so im new in php and sql, and I have this form that submits some names and cities into a database.
I managed to do it, but once a hit the submit button, i get an error:
"Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1"
but, when i check in phpmyadmin, the new record is there!!, so im not sure what's wrong, thats the problem.
this is the code:
<?php
mysql_connect("localhost", "name", "pass") or die(mysql_error());
echo "Connection to the server was successful!<br/>";
mysql_select_db("db_name") or die(mysql_error());
echo "Database was selected!<br/>";
$resultComuna = mysql_query("SELECT idComuna, nombre FROM comuna ORDER BY nombre ASC");
$resultGiro = mysql_query("SELECT idGiro, nombre FROM giro ORDER BY nombre ASC");
?>
<html>
<head>
<title>TEST</title>
</head>
<body>
<br/><br/>
<form name="form" method="POST" action="test_action.php">
<div align="center">
<!--///////////////// input nombre //////////////////////// -->
NOMBRE CLIENTE:
<input name="nombreCliente" type="text" maxlength="30" size="40"></>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////////////drop box para giro ///////////////////// -->
GIRO:
<select name="giro">
<?php
while($row = mysql_fetch_assoc($resultGiro)){
echo "<option value=\"".$row['idGiro']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ///////////////////////////////////////////////////////////// -->
<!-- ////////////// dropbox para comunas //////////////////////// -->
COMUNA:
<select name="comunas">
<?php
while($row = mysql_fetch_assoc($resultComuna)){
echo "<option value=\"".$row['idComuna']."\">".$row['nombre']."</option><br/>";
}
?>
</select>
<!-- ////////////////////////////////////////////////////////////// -->
<input type="submit" value="Ingresar"> </>
</div>
</form>
</body>
</html>
and the test_action.php is:
<?php
$con = mysql_connect("localhost", "name", "pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data_base", $con);
$query = mysql_query("SELECT max(idNombre)+1 as id FROM nombre");
$row = mysql_fetch_array($query);
$idMax = $row['id'];
$sql = mysql_query("INSERT INTO nombre VALUES ('".$idMax."','".$_POST['comunas']."',".$_POST['giro'].",'".$_POST['nombreCliente']."')");
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
mysql_close($con)
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 ID 用单引号插入:
您能提供表结构吗? ID是整数还是varchar呢?
You're inserting the ID in single quotes:
Can you provide the table structure? ID is an integer or a varchar there?
尝试将 test_action.php 更改为:
它有助于调试
Try changing test_action.php to:
It helps for debugging