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.
Related
My query is not working when I use the variable in the WHERE clause. I have tried everything. I echo the variable $res, it shows me the perfect value, when I use the variable in the query the query is not fetching anything thus mysqli_num_rows is giving me the zero value, but when I give the value that the variable contains statically the query executes perfectly. I have used the same kind of code many times and it worked perfectly, but now in this part of module it is not working.
Code:
$res = $_GET['res']; // I have tried both post and get
echo $res; //here it echos the value = mahanta
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'"; // Here it contains the problem I have tried everything. Note: restaurant name is same as it is in the database $res contains a value and also when I give the value of $res i.e. mahanta in the query it is then working.
$z = mysqli_query($conn, $query);
$row2 = mysqli_fetch_array($z);
echo var_dump($row2); // It is giving me null
$num = mysqli_num_rows($z); // Gives zero
if ($num > 0) {
while ($row2 = mysqli_fetch_array($z)) {
$no = $row2['orders'];
$id = $res . $no;
}
}
else {
echo "none selected";
}
As discussed in the comment. By printing the query var_dump($query), you will get the exact syntax that you are sending to your database to query.
Debugging Tip: You can also test by pasting the var_dump($query) value in your database and you will see the results if your query is okay.
So update your query syntax and print the query will help you.
$query = "SELECT * FROM `seller` WHERE `restaurant` = '$res'";
var_dump($query);
Hope this will help you and for newbies in future, how to test your queries.
Suggestion: Also see how to write a mysql query syntax for better understanding php variables inside mysql query
The problem is the way you're using $res in your query. Use .$res instead. In PHP (native or framework), injecting variables into queries need a proper syntax.
I've been having problems trying to update a value based off a code that is received on the page.
For example:
http://example.com/register.php?code=fa82f82712d1 (not the actual code, it's always a 32-char code actually).
I start a transaction, and do an update in the following way:
$stmt = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
Then do a $stmt->execute(array(':code' => $code)); from the code gotten before.
But it never updates anything, I'm running a rowCount() (gives me '0') and closing the transaction but can't seem to get it updated.
The column type is CHAR(32) which should match with the length of the code received.
Is it possible that it's causing confusions because of the data type?
Maybe bind the parameter before like:
$stmt2 = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
$stmt2->bindParam(":code", $code, PDO::PARAM_STR);
if ($stmt2->execute()){
$stmt->commit();
} else {
$stmt->rollBack();
}
EDIT: after comment of other people. The basic is to not use
$stmt = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
but to give different var name like
$stmt2 = $stmt->prepare("UPDATE USER SET GOT_CODE = 1 WHERE CODE = :code");
because otherwise the system will overwrite $stmt and not work anymore
I have a piece of code which takes the keywords out of a url from a database. The code then updates a field within the database with the keywords. The code gets the keywords from the url successfully and then echo's them out on to the page perfectly.
However when I try and update them in to the database, the field doesn't update. If i swap out the variable $c for a word such as test this works perfectly and so the issue is with the variable $c, however i have tried to do this several different ways but i just can not get the field to update.
$result = mysql_query("SELECT * FROM ip_stats1 WHERE page LIKE 'http://www.google.com/uds/afs?q=%' AND id='44'")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$keyword = $row['page'];
$id = $row['id'];
$a = "$keyword";
$b = substr($a, strpos($a, '=') + 1);
print $b;
$c = substr($b, 0, strpos($b, '$'));
echo "$c";
$result2 = mysql_query("UPDATE ip_stats1 SET string1 = '$c' WHERE id = '44'")
or die(mysql_error());
UPDATE
ok, so if I echo $c i get the keywords, so it would simply be for example something like Dough Maker, so that works perfectly. i have also tried writing it like this.
$test = $c;
and then test returns the same result. Originally this was a loop and i took all of the results fro the database to produce a long list, but in an effort to try and find the error i removed the loop to just return one result.
OK, firstly thanks for all your suggestions, I tried various things and then in the end simply realised that actually this was a silly error. the variable $b holds the information i want and as soon as i inserted this to the database everything worked fine.
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)
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).