Reputation: 31
this is my first question here and is really dumb.. but I cant get this workig in spite Ive done ir before, dunno if I take the bad pill or what, please help! Here is my code:
enter code here
<?php
session_start();
include '../conexion.php';
$nombre=$_POST['Nombre'];
$apellido=$_POST['Apellido'];
$mail=$_POST['Mail'];
$telefono=mysqli_real_escape_string($con,$_POST['Telefono']);
$ultimaventa=$_POST['Numeroventa'];
$totalcomprado=0;
$ultimomonto=$_POST['Total'];;
$resultado=mysqli_query($con,"select * from Clientes")or die(mysqli_error($con));
$existe=false;
while($f=mysqli_fetch_array($resultado)){
if($f['Mail']==$mail){
if($f['totalcomprado']==NULL){
$totalcomprado=$ultimomonto;}else{$totalcomprado=$f['totalcomprado']+$ultimomonto;}
mysqli_query($con,"update Clientes SET nombre='".$nombre."', apellido='".$apellido."',Mail='".$mail."',telefono='".$telefono."',ultimaventa='".$ultimaventa."',ultimomonto='".$ultimomonto."',totalcomprado='".$totalcomprado."'")or die(mysqli_error($con));
}else{
$totalcomprado=$ultimomonto;
mysqli_query($con,"insert into clientes(nombre,apellido,Mail,telefono,ultimaventa,ultimomonto,totalcomprado)values(0,
'".$nombre."','".$apellido."','".$mail."','".$telefono."','".$ultimaventa."','".$ultimomonto."','".$ultimomonto."')")or die(mysqli_error($con));}
}
The problema is that the "update part"(when mail is already in database) everything works fine, but when I go to the insert statement nothing happens, nothing inserted, no mysql error, no nothing. All variables have proper values and all data is collected correctly, why i cant insert the data????PS: I tryed putting only the insert statement alone and ye nothing happens...
Upvotes: 0
Views: 67
Reputation: 12795
Your INSERT query has 7 columns in the fields clause, but provides 8 values (note that you have an extra 0 at the beginning of the list of values, which doesn't have a matching column name in the list of fields). It certainly produces an error, you are just not properly catching it.
To quickly fix it, just remove the 0,
part in the VALUES
clause, but I would recommend figuring out why the error is not seen.
EDIT: and as Josan Iracheta properly pointed out, in MySQL table names are case sensitive, so your table name in the INSERT
query needs to begin with a capital letter too.
EDIT2: to be very specific, try this:
mysqli_query($con,"insert into Clientes(nombre,apellido,Mail,telefono,ultimaventa,ultimomonto,totalcomprado)values(
'".$nombre."','".$apellido."','".$mail."','".$telefono."','".$ultimaventa."','".$ultimomonto."','".$ultimomonto."')")or die(mysqli_error($con));}
Also, please note that you have several other problems in your code: your code is vulnerable to SQL injections (try using prepared statements to address it), and also your update query doesn't have WHERE
clause, so you update all the rows every time, not just the one that has matching email address.
EDIT4: Now that I looked at your code more closely, your problem not in SQL, it is in PHP -- your logic for running the INSERT query seems to be wrong, you run it if your table has a row with a different email, not if it doesn't have a row with the email you want. Try changing your code like this:
<?php
session_start();
include '../conexion.php';
$nombre=$_POST['Nombre'];
$apellido=$_POST['Apellido'];
$mail=$_POST['Mail'];
$telefono=mysqli_real_escape_string($con,$_POST['Telefono']);
$ultimaventa=$_POST['Numeroventa'];
$totalcomprado=0;
$ultimomonto=$_POST['Total'];;
$resultado=mysqli_query($con,"select * from Clientes WHERE Mail='".$mail."'")or die(mysqli_error($con));
$existe=false;
if (mysqli_num_rows($resultado) == 0) {
$totalcomprado=$ultimomonto;
mysqli_query($con,"insert into clientes(nombre,apellido,Mail,telefono,ultimaventa,ultimomonto,totalcomprado)values(
'".$nombre."','".$apellido."','".$mail."','".$telefono."','".$ultimaventa."','".$ultimomonto."','".$ultimomonto."')")or die(mysqli_error($con));
}
while($f=mysqli_fetch_array($resultado)){
if($f['Mail']==$mail){
if($f['totalcomprado']==NULL){
$totalcomprado=$ultimomonto;}else{$totalcomprado=$f['totalcomprado']+$ultimomonto;}
mysqli_query($con,"update Clientes SET nombre='".$nombre."', apellido='".$apellido."',Mail='".$mail."',telefono='".$telefono."',ultimaventa='".$ultimaventa."',ultimomonto='".$ultimomonto."',totalcomprado='".$totalcomprado."' WHERE Mail='".$mail."'")or die(mysqli_error($con));
}
}
Note that I also added the WHERE
clause to the SELECT
and UPDATE
statements, remove them if it is not what you actually want there. I also did not address all the SQL-injection issues in your code.
Upvotes: 1