Jump to content

Insert Into Same Row Of Sql Table With Seperate Insert Commands


miocene
 Share

Recommended Posts

I want to insert some data into my sql table.I then want to run a loop that fills in the remaining fields in the row.Here is my code:

include("connection.php");$sql="INSERT INTO payments (companyname, item, date)VALUES('" . mysql_real_escape_string(htmlspecialchars($company)) . "','" . mysql_real_escape_string(htmlspecialchars($item)) . "', '$thedate')";for ($i=0; $i<=$num_users - 1; $i++){//bracket2$username = $users[$i];//determines whether the user is paying or notif (in_array($username, $whopays)) {$user_fs[$i] = $fs;}		else {$user_fs[$i] = 0;}		$topay[$i] = $user_fs[$i] - $personpaid[$i];$sql="INSERT INTO payments (payed{$i}, fs{$i}, topay{$i})VALUES('{$personpaid[$i]}', '{$user_fs[$i]}' , '{$topay[$i]}')";}//bracket2

This inserts the companyname, item and date into 1 row.I then only get the fields filled in from the loop where $i is the last number in the loop.I want all this info in the same row

Link to comment
Share on other sites

First, your second query needs to be an update, not an insert, if you want to add it to the same row as the previous query. Second, I don't see you actually running either query, just building them.
this isn't the whole code but it still seems to put info into the table.how do I run the query then?
Link to comment
Share on other sites

You send the SQL query to the mysql_query function.mysql_query($sql);Make sure to do it after you build the query, if you're building the query inside a loop, make sure you also run it inside the loop.
OK so should my code therefore be?:
include("connection.php");$sql="INSERT INTO payments (companyname, item, date)VALUES('" . mysql_real_escape_string(htmlspecialchars($company)) . "','" . mysql_real_escape_string(htmlspecialchars($item)) . "', '$thedate')";mysql_query($sql);for ($i=0; $i<=$num_users - 1; $i++){//bracket2$username = $users[$i];//determines whether the user is paying or notif (in_array($username, $whopays)) {$user_fs[$i] = $fs;}		else {$user_fs[$i] = 0;}		$topay[$i] = $user_fs[$i] - $personpaid[$i];$sql="UPDATE INTO payments (payed{$i}, fs{$i}, topay{$i})VALUES('{$personpaid[$i]}', '{$user_fs[$i]}' , '{$topay[$i]}')";mysql_query($sql);}//bracket2

Link to comment
Share on other sites

OK the above was clearly not correct...I did:

include("connection.php");$sql="INSERT INTO payments (companyname, item, date)VALUES('" . mysql_real_escape_string(htmlspecialchars($company)) . "','" . mysql_real_escape_string(htmlspecialchars($item)) . "', '$thedate')";mysql_query($sql);$result = mysql_query("SELECT * FROM payments ORDER BY id DESC LIMIT 1");while($row = mysql_fetch_array($result)){$id = $row('id');}for ($i=0; $i<=$num_users - 1; $i++){//bracket2$username = $users[$i];//determines whether the user is paying or notif (in_array($username, $whopays)) {$user_fs[$i] = $fs;}		else {$user_fs[$i] = 0;}		$topay[$i] = $user_fs[$i] - $personpaid[$i];$sql="UPDATE payments SET payed{$i} = '{$personpaid[$i]}', fs{$i} = '{$user_fs[$i]}', topay{$i} = '{$topay[$i]}'WHERE id = $id;mysql_query($sql);}//bracket2

But it still writes 2 seperate records and only writes the $personpaid, $user_fs and $topay for the lastt value of $iEDIT: Nevermind, I made a few punctuation errors and uploaded the wrong file on my ftp - corrected and all works now!

Edited by miocene
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...