0

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\n";
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...

2
  • MySQL table and column names should not be single quoted. Change to either backticks -> $sql = "INSERT INTO `accountmanager` or just leave it unquoted -> $sql = "INSERT INTO accountmanager Commented Dec 1, 2014 at 19:52
  • Thanks dudes, but i have other problem, i don't know why this error appears: Could not enter data: No database selected the schema is Data Manager the table is accountmanager Commented Dec 1, 2014 at 20:03

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

When defining the table and columns you're going to insert to, you can't use a ', you have to use backticks instead.

So your query should be like so:

"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')";

Also, you shouldn't use the mysql extension since it's deprecated and will be removed in the future. You should use mysqli or PDO instead

Comments

0

why use ' ' for table name and values?

try this

$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')";

3 Comments

This is not it at all.
Thanks dudes, but i have other problem, i don't know why this error appears: Could not enter data: No database selected the schema is Data Manager the table is accountmanager
@ashkufaraz Look at David's answer for why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.