richarro1234 Posted September 19, 2009 Share Posted September 19, 2009 Hello, Im trying to insert multiple rows into a mysql database but cant seem to get anything to work. Basically, without goin into detail, the page is called using ajax and it works fine, but im trying to get a list of ID's from a table, put it into an array (maybe?!) then seperate those ID's and add a new row into a table for each ID. I.E im trying to send out a mass message to people on a list, so i query the database for the ID's of people who are on the list, then i need to be able to get those ID's from the query, and insert a new row into the database for each of those ID's. How do i do that? Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/ Share on other sites More sharing options...
khr2003 Posted September 19, 2009 Share Posted September 19, 2009 It is better if you posted the code. But basically when query the database you can add the id into an array: $result = mysql_query("Select * FROM table"); while ($row = mysql_fetch_array($result)) { $id_array[] = $row['id']; } then when you want to send a message for each id you use the for each loop: foreach($id_array as $id) { $result = mysql_query("INSERT INTO table values ($id,$another_var)"); } [/code[ Quote Link to comment https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/#findComment-921019 Share on other sites More sharing options...
mikesta707 Posted September 19, 2009 Share Posted September 19, 2009 you do it exactly as you described doing it. are you asking how the queries go? what PHP you should use? or do you need an entire script? Do you have any code you can post to tell us where you are exactly with this project Quote Link to comment https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/#findComment-921059 Share on other sites More sharing options...
richarro1234 Posted September 19, 2009 Author Share Posted September 19, 2009 Hey, sorry for not posting this the first time, i was quite tired and it just slipped my mind. Here is the full working page i have at the moment, i have removed my attempt a tmultiple inserts as it stopped the whole script from inserting anything. <?php $dbhost = "localhost"; $dbuser = "user"; $dbpass = "pass"; $dbname = "dbname"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); $query1 = mysql_query("SELECT * from users where username = '".$_COOKIE["twusername"]."'") or exit( mysql_error() ); $r = mysql_fetch_array($query1); $friendid = $r['id']; $query2 = mysql_query("SELECT * from friends where myid = '$friendid'") or exit( mysql_error() ); $friendlist = mysql_fetch_array($query2); $friend_feed = $friendlist['friendid']; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ClientIP = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ClientIP = $_SERVER['REMOTE_ADDR']; } //$ClientHost = gethostbyaddr($ClientIP); $ClientAgent = $_SERVER['HTTP_USER_AGENT']; $MyTimeStamp = time(); // Retrieve data from Query String $status = $_GET['status']; $id = $r['id']; if ($sex = 'Male'){ $sex = 'his'; }else{ if ($sex = 'Female') $sex = 'her'; } // Escape User Input to help prevent SQL Injection $status = mysql_real_escape_string($status); //build query $query = "UPDATE users SET mystatus = '$status' WHERE username = '".$_COOKIE["twusername"]."'"; $query1 = "INSERT INTO `feed` (`id`, `userid`, `friendid`, `action`, `did`, `when`, `groupname`) VALUES ('', '$id', '$friend_feed', 'status', 'Updated $sex', '$MyTimeStamp', '$status')"; //Execute query // Exit if calling directly the script file! if ($status != "") { $qry_result = mysql_query($query) or die(mysql_error()); $qry_result = mysql_query($query1) or die(mysql_error()); echo "<b><font color='#FF0000'>Status Updated Successfully</font></b>"; } else { echo '<b>Hacking Attempt!!</b><br><br>'; } ?> I need the whole code to make it work, that works now, but it only inserts one row with no friend_id. Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/#findComment-921107 Share on other sites More sharing options...
richarro1234 Posted September 20, 2009 Author Share Posted September 20, 2009 anyone willing to help me sort this out? Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/#findComment-921659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.