This code does not update my mysql.

//the $q is sent from ajax and looks like this
thumbsup.546 or thumbsdown.567

//the $q is sent trough a working ajax function and looks like this
<a href="#" onclick="thefunction(thumbsup.'.$row['wallid'].');">[+]</a>


$q = mysql_real_escape_string($_GET['q']);

$findme   = 'thumbsup';
$pos = strpos($q, $findme);

if($pos == TRUE){
$thumb  = $q;
$end = end(explode('.', $q));
mysql_query("UPDATE linkwall SET thumbsup=thumbsup +1,thumbuserid='$userid' WHERE wallid ='$end' LIMIT 1");

}

$findme   = 'thumbsdown';
$pos = strpos($q, $findme);
if($pos == TRUE){

$thumb  = $q;
$end = end(explode('.', $q));
mysql_query("UPDATE linkwall SET thumbsdown=thumbsdown +1,thumbuserid='$userid' WHERE wallid ='$end' LIMIT 1");

}

Dani AI

Generated

Short answer: the most common causes here are how strpos() is tested and calling end() on the return value of explode(). was right that strpos was the issue, and was right to suggest surfacing DB errors while debugging.

Why it failed

  • strpos() returns either an integer (0,1,2...) or boolean false. Testing with equality to TRUE will fail when the match is at position 0. Use a strict check against false (or parse the string) instead.
  • end(explode('.', $q)) is fragile because end() requires a reference. Assign the explode() result to a variable first, or use direct array access.
  • Also verify the JavaScript call actually sends a string (e.g. thefunction('thumbsup.567')), otherwise the server may receive garbage or nothing.
  • Treat the ID as an integer (cast to (int)) or use prepared statements; escaping strings alone is not sufficient and the old mysql_* extension is deprecated.

Minimal, safer parsing pattern

$parts = explode('.', $q, 2);
$action = isset($parts[0]) ? $parts[0] : '';
$id = isset($parts[1]) ? (int)$parts[1] : 0;

if ($id > 0 && ($action === 'thumbsup' || $action === 'thumbsdown')) {
    // run an UPDATE using a prepared statement (mysqli or PDO)
}

Further reading

If problems persist, log the raw $q and the SQL error (as suggested) to see the actual failure.

Recommended Answers

All 2 Replies

Try:

mysql_query('UPDATE ...') or die(mysql_error());

i fixed it, but it was something about the strpos that didnt work for me.
Have a great day pritaeas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.