Jump to content

Help with php/mysql update


richarro1234

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/174765-help-with-phpmysql-update/
Share on other sites

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[

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.