简体   繁体   中英

php: 2 mysql queries but only 1 is executed

I have a class in php that needs to send data from a form to a database. The query is split up in 2 queries bcs half the data needs to be send to an other table in the same database.

Now the problem: When I confirm the form then only the data of the sec query have been send to the database but not the data of the first query.

this is what I have:

(database connection) ...

if (something is empty)
{
 Give error.
}
else {

$query = $this->db->prepare("INSERT INTO Table1(coloumn1, coloumn2, coloumn3, coloumn4, coloumn5, coloumn6, coloumn7, coloumn8) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");

    $query->bindParam(1, $Val1);
    $query->bindParam(2, $Val2);
    $query->bindParam(3, $Val3);
    $query->bindParam(4, $Val4);
    $query->bindParam(5, $Val5);
    $query->bindParam(6, $Val6);
    $query->bindParam(7, $Val7);
    $query->bindParam(8, $Val8);

    $query->execute();

    $query = $this->db->prepare("INSERT INTO Table2(coloumn1, coloumn2, coloumn3, coloumn4, coloumn5) VALUES(?, ?, ?, ?, ?)");

    $query->bindParam(1, $Val1);
    $query->bindParam(2, $Val9);
    $query->bindParam(3, $Val10);
    $query->bindParam(4, $Val11);
    $query->bindParam(5, $Val12);

    $query->execute();
  }

What I have done:

  • checked database connection
  • checked table name
  • some error checks
  • trying to make a different function for the sec query (but then he doesn't send anything anymore)
  • delete the sec query (but it still wont send the first query)
  • and ofc googling

I think i am doing something wrong with my first query but I don't know what.


Found it misspelled a column name (yes i did check it 2 times but still didn't saw it).

I believe you used the bind_param method in the wrong way. As stated in http://php.net/manual/en/mysqli-stmt.bind-param.php :

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )

So, assuming all your variables are integers, you could bind them in the following way:

$query->bind_param('iiiiiiii', $Val1, $Val2, $Val3, $Val4, $Val5, $Val6, $Val7, $Val8);

Here are the type specification chars:

  • 'i': corresponding variable has type integer
  • 'd': corresponding variable has type double
  • 's': corresponding variable has type string
  • 'b': corresponding variable is a blob and will be sent in packets

Therefore the solution I propose you is:

$query = $this->db->prepare("INSERT INTO Table1(coloumn1, coloumn2, coloumn3, coloumn4, coloumn5, coloumn6, coloumn7, coloumn8) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");

$query->bind_param('iiiiiiii', $Val1, $Val2, $Val3, $Val4, $Val5, $Val6, $Val7, $Val8);

$query->execute();

$query = $this->db->prepare("INSERT INTO Table2(coloumn1, coloumn2, coloumn3, coloumn4, coloumn5) VALUES(?, ?, ?, ?, ?)");

$query->bind_param('iiiii', $Val1, $Val9, $Val10, $Val11, $Val12);

$query->execute();

bind_param isn't supposed to be used the way you were using it for mysqli:

mysqli_stmt_bind_param documentation

The first parameters is a hint to PHP about what type of variable you are dealing with ( i =>integer, s => string, etc.)

You basically have to give ALL parameters in one line, using VARIABLES (you can't use constants).

--- SIDE NOTE: ADVANCED ---

If your code doesn't know how many parameters to pass at compile time, you might need to use:

call_user_func_array(array($query,'bind_param'), $all_prm );

where $all_prm is a an array of references to the values, first element being the types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM