我的代码存在很大问题,我不知道为什么我不能将新值插入数据库(Mysql),代码是:
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$tutorial_title = addslashes ($_POST['tutorial_title']);
$tutorial_author = addslashes ($_POST['tutorial_author']);
}
else
{
$tutorial_title = $_POST['tutorial_title'];
$tutorial_author = $_POST['tutorial_author'];
}
$submission_date = $_POST['submission_date'];
&#34;错误&#34;
$sql = "INSERT INTO 'accountmanager'('cuenta', 'emisora', 'serie', 'fecha_compra', 'titulos', 'pc', 'total', 'comision',
'total_con_com', 'f.v', 'dias', 'precio_venta', 'total_de_venta', 'comision_de_venta', 'total_com', 'utilidad',
'monto_total', 'Borrar')".
"VALUES".
"('$cuenta', '$emisora', '$serie', '$fecha_compra', '$titulos', '$pc', '$total', '$comision',
'$total_con_com', '$f_v', '$dias', '$precio_venta', '$total_de_venta', '$comision_de_venta', '$total_com', '$utilidad',
'$monto_total', '$Borrar')";
mysql_select_db('TUTORIALS');
$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="600" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="250">Cuenta</td>
<td>
<input name="cuenta" type="text" id="cuenta">
</td>
</tr>
<tr>
<td width="250">Emisora</td>
<td>
<input name="emisora" type="text" id="emisora">
</td>
</tr>
<tr>
<td width="250">Serie</td>
<td>
<input name="serie" type="number" id="serie">
</td>
</tr>
<tr>
<td width="250">Date [ yyyy-mm-dd ]</td>
<td>
<input name="fecha_compra" type="text" id="fecha_compra">
</td>
</tr>
<tr>
<td width="250">Titulos</td>
<td>
<input name="titulo" type="" id="titulo">
</td>
</tr>
<tr>
<td width="250">Precio Compra</td>
<td>
<input name="pc" type="number" id="pc">
</td>
</tr>
<tr>
<td width="250">Total</td>
<td>
<input name="total" type="number" id="total">
</td>
</tr>
<tr>
<td width="250">Comision</td>
<td>
<input name="comision" type="number" id="comision">
</td>
</tr>
<tr>
<td width="250">Total Con Comision</td>
<td>
<input name="total_con_com" type="number" id="total_con_com">
</td>
</tr>
<tr>
<td width="250">Fecha Venta </td>
<td>
<input name="f_v" type="number" id="f_v">
</td>
</tr>
<tr>
<td width="250">Dias</td>
<td>
<input name="dias" type="number" id="dias">
</td>
</tr>
<tr>
<td width="250">Precio Venta</td>
<td>
<input name="precio_venta" type="number" id="precio_venta">
</td>
</tr>
<tr>
<td width="250">Total de Venta</td>
<td>
<input name="total_de_venta" type="number" id="total_de_venta">
</td>
</tr>
<tr>
<td width="250">Comision de Venta</td>
<td>
<input name="comision_de_venta" type="number" id="comision_de_venta">
</td>
</tr>
<tr>
<td width="250">Total Comision</td>
<td>
<input name="total_de_venta" type="number" id="total_de_venta">
</td>
</tr>
<tr>
<td width="250">Utilidad</td>
<td>
<input name="utilidad" type="number" id="utilidad">
</td>
</tr>
<tr>
<td width="250">Monto Total</td>
<td >
<input name="monto_total" type="number" id="monto_total">
</td>
</tr>
<tr>
<td width="250"> </td>
<td>
<input name="add" type="submit" id="add" value="Add">
</td>
</tr>
</table>
</form>
这是我的DB:
INSERT INTO `accountmanager`(`cuenta`, `emisora`, `serie`, `fecha_compra`, `titulos`, `pc`, `total`, `comision`, `total_con_com`, `f_v`, `dias`, `precio_venta`, `total_de_venta`, `comision_de_venta`, `total_com`, `utilidad`, `monto_total`, `Borrar`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],[value-11],[value-12],[value-13],[value-14],[value-15],[value-16],[value-17],[value-18])
错误:
无法输入数据:SQL语法中有错误;检查 手册,对应右边的MySQL服务器版本 在&#39; accountmanager&#39;(&#39; cuenta&#39;,&#39; emisora&#39;,&#39; serie&#39 ;,附近使用的语法 &#39; fecha_compra&#39;,&#39; titulos&#39;,&#39; pc&#39;,&#39;在第1行
我将MAMP和MYSQL用于数据库,但我无法连接以插入值或修改数据...
答案 0 :(得分:2)
单引号表示字符串,使用反向标记表示数据库对象。所以不要这样:
INSERT INTO 'accountmanager'('cuenta', ...
使用此:
INSERT INTO `accountmanager`(`cuenta`, ...
字符串值应该用单引号括起来。但是,如果将数据库对象括在单引号中,则会使查询解析器混淆,因为它认为您正在尝试将值插入到字符串文字中,而不是插入表中的列中。
请注意,如果使用参数化查询而不是直接变量,您的查询将变得更加清晰。
答案 1 :(得分:1)
在定义要插入的表格和列时,不能使用'
,而是必须使用反引号。
所以你的查询应该是这样的:
"INSERT INTO `accountmanager`(`cuenta`, `emisora`, `serie`, `fecha_compra`, `titulos`, `pc`, `total`, `comision`,`total_con_com`, `f.v`, `dias`, `precio_venta`, `total_de_venta`, `comision_de_venta`, `total_com`, `utilidad`, `monto_total`, `Borrar`)
VALUES('$cuenta', '$emisora', '$serie', '$fecha_compra', '$titulos', '$pc', '$total', '$comision', '$total_con_com', '$f_v', '$dias', '$precio_venta', '$total_de_venta', '$comision_de_venta', '$total_com', '$utilidad', '$monto_total', '$Borrar')";
答案 2 :(得分:0)
为什么要使用&#39; &#39;表名和值?
试试这个
$sql = "INSERT INTO `accountmanager`('cuenta', 'emisora', 'serie', 'fecha_compra', 'titulos', 'pc', 'total', 'comision',
'total_con_com', 'f.v', 'dias', 'precio_venta', 'total_de_venta', 'comision_de_venta', 'total_com', 'utilidad',
'monto_total', 'Borrar')
VALUES
('$cuenta', '$emisora', '$serie', '$fecha_compra', '$titulos', '$pc', '$total', '$comision',
'$total_con_com', '$f_v', '$dias', '$precio_venta', '$total_de_venta', '$comision_de_venta', '$total_com', '$utilidad',
'$monto_total', '$Borrar')";