First MySQL UPDATE Not Working in PHP - php

Can't figure this one out:
$try1 = mysql_query("UPDATE com_users SET `choice_one` = 'president'");
$try2 = mysql_query("UPDATE com_users SET `choice_two` = 'president'");
echo mysql_error($try1);
echo mysql_error($try2);
That PHP code echoes nothing and properly changes choice_two, while not changing choice_one.
$try2 = mysql_query("UPDATE com_users SET `choice_two` = 'president'");
$try1 = mysql_query("UPDATE com_users SET `choice_one` = 'president'");
echo mysql_error($try1);
echo mysql_error($try2);
That PHP code echoes nothing and properly changes choice_one, while not changing choice_two.
How is it possible that the order of these update commands could cause one not to work at all?

The order of the queries will not matter. If there is additional code you are not showing, please post it...
mysql_error()'s first parameter is not the result resource, but rather the connection resource. So to get the first's error, you need to call it immediately after the first query.
$try1 = mysql_query("UPDATE com_users SET `choice_one` = 'president'");
// Call with no parameter right after the query it relates to.
echo mysql_error();
$try2 = mysql_query("UPDATE com_users SET `choice_two` = 'president'");
echo mysql_error();
Note that you are not using a WHERE clause, all rows will be updated the first time. The second time you try running those queries (unless you have reset your data), there will be no rows needing updating and the query will have no effect (mysql_affected_rows() == 0).

Related

whats wrong on this cod? update mysql with php

This is part of my new script (PHP + MySQL). I can echo $price_exchange_update on output, so it's not empty or uncompilable data for MySQL (varchar-255).
this is worked cod:
mysqli_query($rate_db,"UPDATE rate_prices SET
price_exchange_update = 'test 36'
WHERE ID='1'");
But if I use this:
mysqli_query($rate_db,"UPDATE rate_prices SET
price_exchange_update = '$price_exchange_update'
WHERE ID='1'");
also this:
mysqli_query($rate_db,"UPDATE rate_prices SET
price_exchange_update = ".$price_exchange_update."
WHERE ID='1'");
both of them not work. where am I wrong?
---- Update
I use simplehtmldom to grab date & time from other page. My variable is this:
$price_exchange_update = $html->find("span[id=updateTime]", 0);
echo $price_exchange_update;
and out put is: شنبه ۲۴ خرداد ۱۲:۳۸
I tested, if I use
mysqli_query($rate_db,"UPDATE rate_prices SET
price_exchange_update = 'شنبه ۲۴ خرداد ۱۲:۳۸'
WHERE ID='1'");
It's worked. but when I try to update from the variable, it's not work.

Updating MySQL DB with PHP

I'm using foreach to loop an array and update a MySQL database.
This is my code
foreach($result['getHiscore'] as $highScoreType => $highScoreValues){
$rank = $highScoreValues['rank'];
$lvl = $highScoreValues['lvl'];
$totalXp = $highScoreValues['totalxp'];
mysqli_query($con,"UPDATE Users SET Level("$highScoreType") = $lvl, Xp("$highScoreType") = $totalXp,
WHERE UserID= '1'");
}
i'm trying to conflate the word "level" with the contents of $highScoreType, the column titles in my DB are Leveloverall, Xpoverall, Levelattack, Xpattack and so on so i was planning on keeping the Level/Xp title constant and just changing the key.
This looks fine to me and when i tested the sql with pre-set values it updated fine, however using the variables doesn't update at all. I know that the variables are coming out of the array correctly as when i echo them inline with the foreach they print out in the correct format and order.
Is it my formatting thats the issue or am i doing missing something else?
If you echo the generated SQL query that should help you see any problems in the query.
It looks odd to me: UPDATE Users SET Level("$highScoreType") = $lvl
Shouldn't that just be UPDATE Users SET $highScoreType = $lvl ?
Be aware also that this sort of code is vulnerably to SQL injection attacks so always be wary of what could be in those variables.
To print the query do:
$query = "UPDATE Users SET Level("$highScoreType") = $lvl, Xp("$highScoreType") = $totalXp, WHERE UserID= '1'"
echo $query
mysqli_query($con, $query)

UPDATE not updating via script yet variables exist

I have the following code which should result in an UPDATE occuring yet I see no change on the table row. Any clues as to why? Below the code I list what I have tried. I've stared at this code for hours!
if (in_array( $topicid , $allowed )) {
$query = $db->query("SELECT lastpost FROM table_topics WHERE forumid=$forumid AND topicid=$topicid");
$thelpost = $db->fetch_array($query);
$db->free_result($query);
$lastpost = explode("|", $thelpost['lastpost']);
$initialtime = $lastpost[2];
$timerightnow = time();
$db->query("UPDATE table_topics SET lastpost='$timerightnow|$loginname|$initialtime' WHERE topicid=$topicid AND forumid=$forumid");
}
Tried printing the query:
$query = $db->query("UPDATE table_topics SET lastpost='$timerightnow|$loginname|$initialtime' WHERE topicid=$topicid AND forumid=$forumid");
print $query;
which resulted in output of 1 which I figure is true.
Tried echoing out variables and they all have the expected output. $loginname and other variables were set in preceeding code not shown.
Tried running the UPDATE manually through phpmyadmin with preset variables. Worked.
Yes, CSV delimiting is bad. It's a legacy app that needs overhauling in the long term.

UPDATE sql not affecting database

I have this code. Its returning 1 but there is no change on the database!
<?
include ("../connect.php");
$id = $_REQUEST['id'];
$stat = $_REQUEST['changeTo'];
$prod = $_REQUEST['product'];
echo mysql_query("UPDATE $prod SET STATUS = '$stat' WHERE ID = '$id'");
echo mysql_error();
?>
An error will only be returned on an UPDATE statement if a SQL error occurs. If no rows are affected the query is still successful and reported as such.
Make sure all of the variables used in the query contain valid values and that the query should actually affect any records in your database.
My first thought is that $id doesn't exist, can you manually enter an id that you know exists and try running that once? just to rule it out if nothing else
I added this in the hopes that I could get an answer vote :)
Try the SQL-Statement direct with values set by code.

How to show a link counter change live?

What this code does is taking links from the db and compare it to a keyword, if it compares then KeywordCounter++, and in every time LinkCounter++
I want to type LinkCounter after every link it goes through but in the code I wrote it only shows me after the loop ends (after all the links crosses). How can I see the LinkCounter every time a link is checked?
How will I be able to see live the counter jumps?
<?php //holdes the db connection include('Connect.php');
$KeyWord = 'googoo';
$LinkCounter = "0";
$KeywordCounter = "0";
$query = mysql_query("SELECT * FROM doalgo where Pass != '0'") or die(mysql_error());
while ($info = mysql_fetch_array($query)) {
$id = $info['id'];
$link = $info['link'];
$num_rows = mysql_num_rows($query);
mysql_query("UPDATE doalgo SET Pass = '1' WHERE id = '$id'");
$CurrentFile = file_get_contents($link);
if (!strpos($CurrentFile, $KeyWord)) {
//nothing
} else {
mysql_query("UPDATE doalgo SET Posted = '1' WHERE id = '$id'");
$KeywordCounter++;
}
$LinkCounter++;
if ($id == $num_rows) {
die();
}
}
echo "<br />KeywordCounter: ".$KeywordCounter;
echo "<br />LinkCounter: ".$LinkCounter;
? >
Its better you calculate the average speed of update (for example number of updates per hour) and send just a single integer to the browser every 1 hour.
using jquery you can change the value shown to user with that speed.
If I understand your question correctly, you want the web page to display immediately, then constantly update the LinkCounter display as the SQL queries progress?
If this is a correct understanding, to do this requires AJAX. Your server has to send constant updates to the web browser every time $LinkCounter is updated, then the JavaScript running in the browser will update the display with that information. Obviously, it's a much more complicated thing to do than what your script currently does. It's an entirely different design pattern.
If this is truly something you want to learn to do, there are many books on the subject of AJAX, or google can help you, too.

Categories