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.
Related
I searched almost whole internet, but could not find something similar, yet my question looks so simple.
I have a php code like so:
$id = 1;
if (!isset($_POST['port1'])) {
$port1 = null;
}
... which simply checks if submitted value is empty or not, if it is empty, then variable $port1 = null;
then, further in the code, I need to insert this value/update it in the database
$sql_update = ("UPDATE names_10 SET `digName1`=$port1 WHERE `device_id`='$id'");
...which should set the "digname1" to null. but it won't!
I tried every combination, every type of quotes, but every time I got UPDATE error..
any ideas?
Try this:
$id = 1;
if (!isset($_POST['port1'])) {
$port1 = "NULL";
}
$sql_update = ("UPDATE names_10 SET `digName1`= $port1 WHERE `device_id`='$id'");
I would rather suggest you to use PDO when you plan to bind something like this. There are a lot of benefits using PDO that would amaze you!
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)
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.
I am trying to grab ad code from my database and echo it on to the page, but for some reason it is not showing up?
$getad = ("SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ");
while($rows = mysql_fetch_array($getad))
{
$code = $rows['code'];
}
$ad1 = $code;
later down the page i print it like this.
<?php print $ad1 ?>
I think your problem is that you don't actually execute the query, you just have saved it in a variable ($getad) and then try to do a fetch af an array containing a string as I see it. If I remeber correctly you have to save you query in a variable, as you did, and then type
$getad = "SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ";
$q = $db->query($getad);
// generate results:
while ($q->fetchInto($row)) {
//display or store
}
You should also include checks, for example that this code has extracted at least one row, or that database connection is working, etcetera.
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).