dpeqsfx5186 2014-12-01 19:46
浏览 36
已采纳

PHP Mysql错误,插入值到数据库

i have a big problem with my code, i don't know why i can't insert new values to the database (Mysql ) there is the code :

<?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'];

The "error"

$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
";
mysql_close($conn);
}

The table

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>

and this is my 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])

The error:

Could not enter data: 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 ''accountmanager'('cuenta', 'emisora', 'serie', 'fecha_compra', 'titulos', 'pc', ' at line 1

I use MAMP and MYSQL to the database, but i can't connect to insert values or modify the data...

  • 写回答

3条回答 默认 最新

  • dougou5844 2014-12-01 19:52
    关注

    Single quotes represent a string, use back-ticks to represent a database object. So instead of this:

    INSERT INTO 'accountmanager'('cuenta', ...
    

    Use this:

    INSERT INTO `accountmanager`(`cuenta`, ...
    

    The string values should be enclosed in single-quotes. But if you enclose database objects in single-quotes then that will confuse the query parser because it thinks you're trying to insert values into a string literal, rather than into columns in a table.

    Note that your queries will become a bit cleaner if you use parameterized queries instead of direct variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘