Error increasing the value of the viable Php / sql

1

Good day, from a php form I am capturing the information in phpmyadmin.

In the background is the realization of a purchase that when stored in the database increases the purchase number (purchase 1, 2, 3 etc.).

I have reviewed the code several times and I have not been able to find the error that makes each one make a purchase with the same number. That is, every purchase I make is saved with the same purchase number instead of the purchase number increasing .

I thank you in advance for your help.

  <?php
session_start();
include "verbindung.php";
$arreglo=$_SESSION['car'];
$numeroventa=0;


$re=mysqli_query($verbindung,"select * from bestellposition order by verkauf_num DESC limit 1") or die(mysqli_error($verbindung));
while ( $f=mysqli_fetch_array($re)) {
  $numeroventa=$f['Verkauf_num'];
  $datum=$f['Datum'];
}

if($numeroventa==0){
  $numeroventa=1;
}else{
  $numeroventa=$numeroventa+1;

}
for($i=0; $i<count($arreglo); $i++){
  mysqli_query($verbindung,"insert into bestellposition (verkauf_num,Kunde_ID,Produkt_ID,Menge,name,Summe) values(
    ".$numeroventa.",
    '".$_SESSION['Kunde_ID']."',
    '".$arreglo[$i]['produkt_id']."',
    '".$arreglo[$i]['menge']."',
    '".$arreglo[$i]['name']."',
    '".($arreglo[$i]['preis']*$arreglo[$i]['menge'])."'
    )")or die(mysqli_error($verbindung));
  }

  for($i=0; $i<count($arreglo);$i++){
    mysqli_query($verbindung,"insert into rechnung (bestellposition_ID,Bestellung_Datum) values(
      ".$numeroventa.",
      '".$datum."'
      )")or die(mysqli_error($verbindung));
    }


  unset($_SESSION['car']);
  header("Location: resume_einkauf.php");

  ?>
    
asked by Viatorii 15.06.2017 в 10:37
source

3 answers

2

Good morning,

Consulting the official documentation of mysqli_fecth_array .

  

Definition and Usage The mysqli_fetch_array () function fetches a   result row as an associative array, to numeric array, or both.

     

Note: Fieldnames returned from this function are case-sensitive .

In your source code:

$re=mysqli_query($verbindung,"select * from bestellposition order by verkauf_num DESC limit 1") or die(mysqli_error($verbindung));
while ( $f=mysqli_fetch_array($re)) {
  $numeroventa=$f['Verkauf_num']; 
  $datum=$f['Datum'];
}

You are getting the field $numeroventa=$f['Verkauf_num']; with the V in upper case, when you use it in all lowercase sentences.

See how MySQL is returning this field and write it as MySQL sends it to you.

COMMENT: This as to what may be happening to you, however, I agree with G3l0's answer that the correct use would be to use an auto-incremental.

    
answered by 15.06.2017 в 11:06
1

In the database itself, put the column in question as autoincremental, and when passing data if you pass null should take the next element that touched the sequence.

    
answered by 15.06.2017 в 11:01
0

I want to tell you several things about the code, in the order that I see:

  • SELECT * ... : Poor practice at the optimization level , select only the columns you need, not all the columns in the table.
  • verkauf_num or Verkauf_num What is the column in the table called really ?
  • select * from bestellposition order by verkauf_num DESC limit 1 if you use only to know the last verkauf_num , you can save that query by declaring the column verkauf_num as autoincremental and assigning NULL or not indicating nothing for that column at the moment of insertion. That way, do not either, you would have to execute this code snippet: if($numeroventa==0){ ... , I think it's not the best way to do it. Also, if there is a bug in the code you could have the variable $numeroventa increased in a way unreal . Give the impression that you want to make an auto-incremental hand made (handmade). Why do not you let the database system do it and manage it?
  • In the first while , does this $datum=$f['Datum']; serve something?
  • The two inserts in two loops for ... . They have two problems. One is very serious , since it puts the data in serious risk because such queries are vulnerable to SQL injection. In that sense, see: How to avoid SQL injection in PHP? The other is one with respect to > performance . It would be better to build the inserts inside the loop and then execute them in a single call to the database outside the loop . This is extremely easy and much safer when using prepared queries.
  • Note :

    This does not solve your problem puntual , but checking the code I saw at least those five mistakes and I wanted to point them out, because, even if you solve your problem now , they will give you many headaches in the future . I think you should improve those points and maybe rethink the way you have organized your database.

    I hope it serves you.

        
    answered by 15.06.2017 в 14:00