miocene Posted July 23, 2009 Share Posted July 23, 2009 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 More sharing options...
justsomeguy Posted July 23, 2009 Share Posted July 23, 2009 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. Link to comment Share on other sites More sharing options...
miocene Posted July 23, 2009 Author Share Posted July 23, 2009 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 More sharing options...
justsomeguy Posted July 24, 2009 Share Posted July 24, 2009 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. Link to comment Share on other sites More sharing options...
miocene Posted July 24, 2009 Author Share Posted July 24, 2009 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 More sharing options...
miocene Posted July 24, 2009 Author Share Posted July 24, 2009 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! Link to comment Share on other sites More sharing options...
justsomeguy Posted July 24, 2009 Share Posted July 24, 2009 Instead of doing this:$result = mysql_query("SELECT * FROM payments ORDER BY id DESC LIMIT 1");while($row = mysql_fetch_array($result)){$id = $row('id');}You can just do this:$id = mysql_insert_id();http://www.php.net/manual/en/function.mysql-insert-id.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.