Lucho Mansilla
Lucho Mansilla

Reputation: 31

Errno(0) when failing to insert with prepared statement

I am having an error when inserting data into a table with php using prepared statements, I am stuck cuz it gives me errno(0), I dont know what that error is, can you help me please?. Thanks!

<?php
session_start();
include '../conexion.php';

$nombre        = $_POST['Nombre'];
$apellido      = $_POST['Apellido'];
$mail          = $_POST['Mail'];
$telefono      = $_POST['Telefono'];
$ultimaventa   = $_POST['Numeroventa'];
$totalcomprado = 0;
$ultimomonto   = $_POST['Total'];
if($cons = $mysqli->prepare("select 1 from clientes WHERE Mail=?"));
$cons->bind_param('s',$mail);
$cons->execute();
$cons->store_result();

$existe=$cons->num_rows > 0;

if ($existe) {
    $totalcomprado=totalcomprado+$ultimomonto;
    if(!($cons=$mysqli->prepare("UPDATE clientes SET   nombre=?,apellido=?,Mail=?,telefono=?,ultimaventa=?,ultimomonto=?,totalcomprado= ? WHERE Mail=?"))){
        echo "fallo en la preparacion de la consulta:(".$mysqli->errno.")" .$mysqli->error;
    }
    $cons->bind_param('sssssiis',$nombre,$apellido,$mail,$telefono,$ultimaventa,$totalcomprado,$mail);
    if(!($cons->execute())){
        echo "fallo ejecutando la consulta:(".$mysqli->errno.")" .$mysqli->error;
    }
    $cons->close;

} else {
    $totalcomprado=$ultimomonto;
    if(!($cons=$mysqli->prepare("INSERT into clientes id,nombre,apellido,Mail,telefono,ultimaventa,ultimomonto,totalcomprado values(?,?,?,?,?,?,?)"))){
        echo "fallo en la preparacion de la consulta:(".$mysqli->errno.")" .$mysqli->error;
    }
    $cons->bind_param('sssssis',$nombre,$apellido,$mail,$telefono,$ultimaventa,$totalcomprado);
    if(!($cons->execute())){
        echo "fallo ejecutando la consulta:(".$mysqli->errno.")" .$mysqli->error;

    }
}

Ps.: The data types to insert are ok, the only one Integer is "ultimomonto"

This is the error:

fallo en la preparacion de la consulta:(0)
( ! ) Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\mumushop\compras\verificar.php on line 35

Upvotes: 0

Views: 1382

Answers (1)

Barmar
Barmar

Reputation: 780782

You're missing the parentheses arount the column names in the INSERT statement:

    if(!($cons=$mysqli->prepare("INSERT into clientes (id,nombre,apellido,Mail,telefono,ultimaventa,ultimomonto,totalcomprado) values(?,?,?,?,?,?,?)"))){

There's another problem that isn't related to the error:

if($cons = $mysqli->prepare("select 1 from clientes WHERE Mail=?"));

The ; ends this if statement, so you're not using it to execute anything based on whether this is successful. I think you want all the rest of the code to be inside this, so it should be:

if($cons = $mysqli->prepare("select 1 from clientes WHERE Mail=?")) {
    $cons->bind_param('s',$mail);
    $cons->execute();
    $cons->store_result();

    $existe=$cons->num_rows > 0;

    if ($existe) {
        ...
    } else {
        ...
    }
}

The undefined constant error is coming from this line:

$totalcomprado=totalcomprado+$ultimomonto;

You're missing the $ before totalcomprado, it should be:

$totalcomprado=$totalcomprado+$ultimomonto;

or you can write it as:

$totalcomprado += $ultimomonto;

Upvotes: 1

Related Questions