MySqli insert not working

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 587 Views - Last Post: 22 September 2013 - 10:55 AM Rate Topic: -----

#16 danbywinby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-January 13

Re: MySqli insert not working

Posted 22 September 2013 - 10:16 AM

Am i doing it wrong then cus all i want to do is when a user submits a log i want to be able to insert the log number so for example if it's their first log i want the lognumber to be 1 and for the seconf i want it to be 2 etc.

How can i do this? Because the code i am trying to use now worked before i changed my code to using mysqli but now it's no longer working.
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: MySqli insert not working

Posted 22 September 2013 - 10:18 AM

Perhaps you can post a copy of the code that was working with mysql.
Was This Post Helpful? 0
  • +
  • -

#18 danbywinby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-January 13

Re: MySqli insert not working

Posted 22 September 2013 - 10:24 AM

View Postandrewsw, on 22 September 2013 - 10:18 AM, said:

Perhaps you can post a copy of the code that was working with mysql.


Here is another piece of code in the same file which gets log number but for a different purpose and this insert bit doesn't use mysqli yet.

$sql="INSERT INTO jwtdriversbank2 (DriverID, LogID, TotalProfit, LateFee, FinesCost, TravelExpenses, FuelCost, CargoDamage, TruckDamage) VALUES ('$userID', COALESCE((Select MAX(LogID) from jwtdriversbank2 tab2 where tab2.DriverID = '$userID'),0)+1,'$TotalProfitPost','$LateFeePost', '$FinesCostPost2' , '$TravelExpensesPost2' ,'$FuelCostPost','$CargoDamagePost','$TruckDamagePost')";

Was This Post Helpful? 0
  • +
  • -

#19 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: MySqli insert not working

Posted 22 September 2013 - 10:30 AM

Yes, so that code indicates that you can embed the COALESCE() function in an INSERT statement, even though it's reading the MAX() value from the same table.

So you can adapt your current code to do something similar.
Was This Post Helpful? 0
  • +
  • -

#20 danbywinby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-January 13

Re: MySqli insert not working

Posted 22 September 2013 - 10:32 AM

But i can't because mysqli prepared statements don't allow that
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: MySqli insert not working

Posted 22 September 2013 - 10:55 AM

View Postdanbywinby, on 22 September 2013 - 05:32 PM, said:

But i can't because mysqli prepared statements don't allow that

Are you sure? Try it ;). The previous posts advised that you cannot use a sql-statement as a parameter-value, but you would be modifying the sql-statement itself (not the parameter values).

But if not, then you'll need to go the alternative route, which is to execute a sql-statement to retrieve the MAX+1 value, then insert this as a parameter in your insert statement.

This post has been edited by andrewsw: 22 September 2013 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2