Now, the program is working fine to an extent, in that when mysql_affected_rows is more than 0, it does indeed add the data to the new table and print out the relevant echo message.
However, when mysql_affected_rows = 0, I get nothing, no error message, but absolutely no output at all.
I've stripped the code back, do any of you have any idea, i've looked at brackets, and closing conditions etc and can't work out why!
Code
$query10 = ("SELECT p.surname, p.passNo, p.activeUntil FROM PASSENGER p WHERE p.activeUntil < DATE_ADD(NOW(),INTERVAL -1 DAY)");
$result = mysql_query($query10);
while($row = mysql_fetch_array($result))
{
$surname = $row['surname'];
$passNo = $row['passNo'];
mysql_query("INSERT INTO ARCHIVED_PASSENGER (surname, passNo) VALUES ('$surname', '$passNo') ")
or die(mysql_error());
if (mysql_affected_rows()>0) {
echo '<p>';
echo "The number of rows affected by this update is: ";
echo mysql_affected_rows();
}
if (mysql_affected_rows()<1) {
echo '<p>';
echo "No records were affected. Taking you back to the control panel.";
}
}
I would advice to add a if($result != false && mysql_num_rows($result) > 0) just before the while loop. Like so:
$query10 = ("SELECT p.surname, p.passNo, p.activeUntil FROM PASSENGER p WHERE p.activeUntil < DATE_ADD(NOW(),INTERVAL -1 DAY)");
$result = mysql_query($query10);
if($result != false && mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$surname = $row['surname'];
$passNo = $row['passNo'];
$query11 = mysql_query("INSERT INTO ARCHIVED_PASSENGER (surname, passNo) VALUES ('$surname', '$passNo') ")
or die(mysql_error());
if ($query11 != false && mysql_affected_rows()>0) {
echo '<p>';
echo "The number of rows affected by this update is: ";
echo mysql_affected_rows();
echo '</p>';
}
if ($query11 == false || mysql_affected_rows()<1) {
echo '<p>';
echo "No records were affected. Taking you back to the control panel.";
echo '</p>';
}
} else {
//nothing was retrieved, give some error!
}
}
Related
I have a problem to adding more strings in my database.
The idea is: SELECT information, then added array together, after these UPDATE to database.
These are in one code, but UPDATE not working with summed arrays only separately.
With echo I see the array_unshift is working well, the data is good, but not updating.
Need I change something on the server? Maybe version?
(I don't get mysqli_error!)
//CHECKBOX KIOLVASÁSA DB-BŐL!
$sql = ("SELECT id, checkbox FROM osszesito WHERE id = '$id'");
//$result = mysqli_query($conn, $sql);
//if (mysqli_num_rows($result) > 0) {
if ($result = mysqli_query($conn, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
//EREDETI SOR LISTÁZÁSA
$original_array = array( $row["checkbox"] );
$x ="";
echo 'Eredeti sor: ';
foreach ($original_array as $x)
{
echo "$x "."<br><br>";
}
//EREDETI SOR KIEGÉSZÍTÉSE AZ ÚJ ADATTAL
array_unshift($original_array, $chb);
$last ="";
echo "Új sor: "."<br>";
foreach ($original_array as $last)
{
echo $last."<br>";
}
//ÚJ SOR FRISSÍTÉSE A DB-BEN!
//$sqla = "UPDATE osszesito SET checkbox = '$chb' WHERE id = '$id' ";
$sqla = "UPDATE osszesito SET checkbox = '$last' WHERE id = '$id' ";
if (mysqli_query($conn, $sqla)) {
echo "ÚJ SOR ELMENTVE!";
//header("Location: /megrendelesek/index.php");
} else {
echo "Hiba a beírás során: " . mysqli_error($conn);
}
}
///////////////////////////////////////////////
//LEZÁRÁS
} else {
echo "Jelenleg nincs megrendelés az adatbázisban!";
}
mysqli_close($conn);
I am executing two queries and evaluating the following conditions for each record:
if $production_query->row->0 is equal to $jobcard_query->row->0
if $production_query->row->1 is equal to $jobcard_query->row->1
When true, it should display the results of $production_query.
However, when using a while statement, the browser takes a long time to respond and crashes.
Can anyone suggest a solution?
My code:
$query = " SELECT job_card_num , die_qty,id FROM sample_jobcard ORDER BY id DESC”
$production_query = mysql_query($query,$connection1);
$query1 = "SELECT job_card_num , die_qty,id FROM com_jobcard ORDER BY id DESC ";
$jobcard_query = mysql_query($query1,$connection1);
while ($row = mysql_fetch_array($production_query))
{
while( $row1 = mysql_fetch_array($jobcard_query))
{
while (($row1[0] == $row[0]) && ($row1[1] == $row[1]))
{
echo $row[0] . $row[1]. $row[2]'<br>';
}
}
}
Try changing the query to something like this:
$query = "SELECT sample_jobcard.job_card_num AS JOBCARDNUM, sample_jobcard.die_qty AS DIEQTY, sample_jobcard.id AS JID, com_jobcard.job_card_num, com_jobcard.die_qty, com_jobcard.id FROM sample_jobcard
join com_jobcard on sample_jobcard.job_card_num = com_jobcard.job_card_num AND sample_jobcard.die_qty = com_jobcard.die_qty ORDER BY sample_jobcard.id DESC";
$jobcard_query = mysql_query($query);
if($jobcard_query && mysql_num_rows($jobcard_query) > 0)
{
while ($row = mysql_fetch_array($jobcard_query))
{
echo $row['JOBCARDNUM']." ".$row['DIEQTY']." ".$row['JID']."<br>";
}
}
Replace while:
while (($row1[0] == $row[0]) && ($row1[1] == $row[1]))
{
echo $row[0] . $row[1]. $row[2]'<br>';
}
With if:
if ($row1[0] == $row[0] && $row1[1] == $row[1])
{
echo $row[0] . $row[1]. $row[2] . '<br>';
}
If your conditions ever evaluate as true, you have essentially written:
while(true) {
// run forever
}
This will run forever (well, until maximum execution time).
You end up printing the same line over and over, producing a large document that your browser has difficulty handling, inducing a crash.
my problem is that I want to display the messages between two users and order them by time.
I nmanaged to display the messages from each user but it displays first for the user George and the for user Niklakis as it shows in the image below...
What I want is to display both of the messages order by time.
This is part of my code...
$query = "SELECT * FROM messages WHERE recip='$view' ORDER BY time DESC";
$query2 = "SELECT * FROM messages WHERE recip='$username' ORDER BY time DESC";
$result = queryMysql($query);
$result2 = queryMysql($query2);
$num = mysql_num_rows($result);
$num2 = mysql_num_rows($result2);
for ($j = 0 ; $j < $num ; ++$j)
{
$row = mysql_fetch_row($result);
if ($row[3] == 0 || $row[1] == $username || $row[2] == $username)
{
echo date('M jS \'y g:ia:', $row[4]);
echo " <a href='messages.php?view=$row[1]'>$row[1]</a> ";
if ($row[3] == 0)
echo "wrote: "$row[5]" ";
else echo "private message: <span class='whisper'>" .
""$row[5]"</span> ";
if ($row[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row[0]'>erase</a>]";
echo "<br>";
}
}
for ($j = 0 ; $j < $num2 ; ++$j)
{
$row2 = mysql_fetch_row($result2);
if ($row2[3] == 0 || $row2[1] == $username || $row2[2] == $username)
{
echo date('M jS \'y g:ia:', $row2[4]);
echo " <a href='messages.php?view=$row2[1]'>$row2[1]</a> ";
if ($row2[3] == 0)
echo "wrote: "$row2[5]" ";
else echo "private message: <span class='whisper'>" .
""$row2[5]"</span> ";
if ($row2[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row2[0]'>erase</a>]";
echo "<br>";
}
}
I can understand that my problem happens because I have two different for loops and I probably need only ONE for loop to do this work. But I tried and I could find a way to do this.
Can you help?
Thank you.
You use the same code for both query results (tip for the future: PHP supports something like functions :D try it out) and your queries have the same structure so why don't you just fecht all entries for both users and sort them? Have you tried this out?
$query = "SELECT * FROM messages WHERE (recip='$username' AND auth='$view') OR (recip='$view' AND auth='$username') ORDER BY time DESC";
the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.
I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.
<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("<div id=norequests>No results for <strong>$q
</strong></div>");
}
elseif ($rows > 0) {
while ($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
$linksys = htmlspecialchars($row['name']);
$pid = htmlspecialchars($row['system_id']);
}
print("");
}
}
else{
echo '<div id="error">No results.</div>';
}
$sql = "SELECT `Friend_id` from `friends_container`
WHERE `System_id` = '$sid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die("Error: " . mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("");
}
elseif ($rows > 0)
{
while ($row = mysql_fetch_array($query))
{
$existing = htmlspecialchars($row['Friend_id']);
if ($existing == $pid) {
echo("<img src=$linksys />$person - Already Existing");
}
else
//Problem code - when i use this echo below this where it repeats 3 times
{
echo("Add $person");
}
}
?>
You must have 3 images stored for each account.
Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.
I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. The database stores many different leads - with each lead being assigned to a specific closer; thus the database stores for each lead the lead id, name, closer name, and other info. The main lead list shows a checkbox next to each lead which submits the lead id into an array:
<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />
Now this all goes to the following page:
<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
?>
I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. Here is where I have no idea how to continue.
Actually - i got it. I don't know why I didn't think of it sooner. First off I passed the original $list variable over to the new page - and then:
<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";
++$i; } } else { echo "The database is empty"; };
?>
The updated page before this:
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";
?>
After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers.
After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them.
Got the idea or you want some code?