• 0

[PHP] Mysql query not Updating, but query works in PHPMyAdmin


Question

This is my PHP code:

  Quote
$Category1 = $_POST['textboxCategory1'];

$Category2 = $_POST['textboxCategory2'];

$Category3 = $_POST['textboxCategory3'];

$Category4 = $_POST['textboxCategory4'];

$Category5 = $_POST['textboxCategory5'];

$con = mysql_connect("intralook.ca","myusername","mypassword

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("intralo1_shop", $con);

$qry = "UPDATE Categories SET CategoryName = '" . $Category1 . "' WHERE CatID=1; " . "UPDATE Categories SET CategoryName = '" . $Category2 . "' WHERE CatID=2; " . "UPDATE Categories SET CategoryName = '" . $Category3 . "' WHERE CatID=3; " . "UPDATE Categories SET CategoryName = '" . $Category4 . "' WHERE CatID=4; " . "UPDATE Categories SET CategoryName = '" . $Category5 . "' WHERE CatID=5; ";

echo $qry;

mysql_query($qry);

mysql_close($con);

The query it outputs is:

  Quote

UPDATE Categories SET CategoryName = 'Item 1' WHERE CatID=1; UPDATE Categories SET CategoryName = 'Item 2' WHERE CatID=2; UPDATE Categories SET CategoryName = 'Item 3' WHERE CatID=3; UPDATE Categories SET CategoryName = 'Item 4' WHERE CatID=4; UPDATE Categories SET CategoryName = 'Item 5' WHERE CatID=5;

When I run that in PHPMyAdmin, it works properly. I do not get any errors when I try to run the PHP code by submitting the form. It shows a blank page (except for the query, which I put an echo statement in to show for debugging).

9 answers to this question

Recommended Posts

  • 0

Nope. I know what's the problem. I've ran to it as well.

Are you using a paid hosting? If so, then it means that the user you created does not have the privileges of Updating. Modify the user at your account. It took me a whole day to figure that out.

Hope that this helps out.

  • 0

Nope, that's not the problem here. The problem is thus:

  Quote
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

Try this (rewritten using MySQL improved, made a bit more secure, and tidied up a bit):

$dh = mysqli_connect("intralook.ca", "myusername", "mypassword", "intralo1_shop");
if (!$dh) die('Could not connect: ' . mysqli_error());

$post = array_map(array($dh, 'real_escape_string'), $_POST);
foreach($post as $key => $value) ${$key} = $value;

mysqli_multi_query($dh,
  "UPDATE `Categories` SET `CategoryName` = '$textboxCategory1' WHERE `CatID`=1;" .
  "UPDATE `Categories` SET `CategoryName` = '$textboxCategory2' WHERE `CatID`=2;" .
  "UPDATE `Categories` SET `CategoryName` = '$textboxCategory3' WHERE `CatID`=3;" .
  "UPDATE `Categories` SET `CategoryName` = '$textboxCategory4' WHERE `CatID`=4;" .
  "UPDATE `Categories` SET `CategoryName` = '$textboxCategory5' WHERE `CatID`=5;"
);

mysqli_close($dh);

  • 0

IM just basic php but pretty sure how your writing teh code is wrong, also your pointing to mysqlquery when you need mysqlmultiquery: http://php.net/manua...multi-query.php

I think this is the way i was shown on a forum a while back when i wanted to do the same as you, its on the link above:

<?php
// WORKING CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK
while ($mysqli->next_result()) {;} // flush multi_queries
$mysqli->query(" SQL statement #1 ; ") // now executed!
$mysqli->query(" SQL statement #2 ; ") // now executed!
$mysqli->query(" SQL statement #3 ; ") // now executed!
$mysqli->query(" SQL statement #4 ; ") // now executed!
?>

//then again it might of been this way

<?php
$link = mysqli_connect('localhost', 'user', 'pw', 'db');

$script = "INSERT INTO T1('pk', 'val') VALUES('1', 'A');";
$script .= "INSERT INTO T1('pk', 'val') VALUES('2', 'B');"
$script .= "INSERT INTO T1('pk', 'val') VALUES('3', 'C');"
$script .= "ALTER TABLE T1 ADD COLUMN `foo` INT(1) NOT NULL;";
$script .= "UPDATE T1 SET val = 'D' where pkkkkkkkkkkkk = '1'"; // Note that this statement will force a SQL Error since column pkkkkkkkkkkkk doesn't exist

mysqli_autocommit($link, false); // set autocommit to false

mysqli_multi_query($link, $script); // execute statements

mysqli_rollback($link); // rollback all statements
?>

Someone way better at php will definitely corrext me though lol

  • 0

The code I posted above will work. The link you posted to the manual is mysqli_multi_query, so it will not work by directly changing mysql_query to mysqli_multi_query as the whole block of code would need to be rewritten in MySQLi (as in the post I made) :)

This topic is now closed to further replies.
  • Posts

    • I simply don't get why this "OS" exists. Who cares? Great, you can now run a Windows 95-era stupid game, super cool...
    • In the UK the standard edition has been priced at £69.99, the deluxe is £89.99, the super deluxe is .... £119.99 where the hell do they suggest these prices are coming from then! Sorry but at these prices I will not be buying when it comes out. It will be much, much later when the price has dropped considerably!
    • It'll just have $300 dlc instead, several season pass and at every moment you'll be left feeling you purchased the demo by buying the standard or deluxe editions, because the superdeluxe is how they meant to release the game in full.
    • Following higher pricing fears, Take-Two confirms Borderlands 4 won't be $80 by Pulasthi Ariyasinghe Video games going even higher in price has been a controversial subject as of late. Following the bump up to $70 in recent years, which has been vastly adopted by many publishers, now the price is slowly creeping up to be $80 for major releases. However, it seems Take-Two's latest game will not be following this new trend, despite what was being said in earlier reports. Take-Two and Gearbox today confirmed the launch of pre-orders for Borderlands 4, and front and center was the pricing information for the standard edition, which is set at $69.99. It seems the cooperative-focused looter shooter is not following in the footsteps of other high-profile $79.99 releases like Mario Kart World from Nintendo and the upcoming RPG The Outer Worlds from Microsoft. As for why many were expecting the Borderlands title to also be $80, Gearbox boss Randy Pitchford had alluded to the price hike recently. In a social media post in May, Pitchford answered a question from a fan regarding the game possibly being $80, to which he replied, saying that it was not his decision. "If you’re a real fan, you’ll find a way to make it happen," he went on to say, defending a possible $10 price hike. "My local game store had Starflight for Sega Genesis for $80 in 1991 when I was just out of high school working minimum wage at an ice cream parlor in Pismo Beach and I found a way to make it happen." In the end, Borderlands 4 is now confirmed to be releasing at $69.99 for the Standard Edition, with pre-orders available now. At the same time, a $99.99 Deluxe Edition and a $129.99 Super Deluxe Edition have also been confirmed, with each higher tier adding more cosmetic customizations as well as post-launch access to unannounced DLC content. Borderlands 4 launches September 12, 2025, across PC (Steam and Epic Games Store), Xbox Series X|S, and PlayStation 5. A Nintendo Switch 2 release is also planned to land later in 2025.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      641
    2. 2
      ATLien_0
      274
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!