• 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

    • Forza Motorsport gets IndyCar, Career mode improvements, and more by Taras Buria Turn 10 Studios announced the next Forza Motorsport update, which includes new content and various game improvements. Update 21 follows the previous Anniversary Update and adds IndyCar, a free expansion for Career mode, Featured Tours, new reward cars, and more. IndyCar is back in the game, allowing you to get behind the wheel of the latest-generation open-wheel racing cars. New vehicles include the 2025 Chevrolet IndyCar and the 2025 Honda IndyCar. There is also the all-new IndyCar Series in Featured Multiplayer. Career mode now has the Champions Cup, a new home for racecar-focused tours in single-player racing. There, you can drive a variety of modern and classic racecars from one-make series to endurance legends. Completing the Champions Cup will give you various high-reward cars like the 1979 BMW #6 BMW Motorsport M1 Procar, the 1998 Nissan #23 Pennzoil NISMO Skyline GT-R, the 2014 Dodge #93 SRT Motorsports Viper GTS-R, and more. Developers also adjusted the in-game pricing for most racecars to make the Champions Cup more accessible. Another change in Update 21 is the return of previous Featured Tours. The game will feature an additional Tour every month, allowing you to earn some of the previous reward cars and participate in the now-ended events. Here is a brief overview of other changes and improvements: Featured Multiplayer has been reorganized, incorporating player feedback, with the goal of providing players with even more event variety. Click here for a rundown of these changes. We’ve implemented BoP Spec changes to the Forza GT2, Forza GT3, Forza Touring Car, and Forza Proto-H divisions with help from participants in the Forza Insiders Program. See the release notes for an overview of the adjustments that have been made. The Challenge Hub menu has been reorganized to more effectively communicate where rewards can be earned as well as the type of rewards available to unlock. We’ve fixed an issue for players using the Fanatec DD2 racing wheel that would assign the peripheral an incorrect mapping profile. All Builders Cup Tours are now immediately unlocked, which means you no longer need to complete the Modern Tour to access the 3 other Builders Cup Tours. In addition, select Builders Cup Series have been updated to feature additional eligible cars and post-launch tracks. A detailed overview of these changes has been included in the release notes. There are also numerous fixes to AI behavior and competitiveness in single-player racing. You can read more about Forza Motorsport Update 21 in a post on the official Forza website.
    • If it ever gets to a reasonable level of parity, I think more real competition in the Windows space would be a good thing. What I like about this is that it's not just another reskinned Linux distro (with all of those inherent problems). It's meant to literally boot and work like Windows NT+. That could be an interesting development one day -- note that this has been in development since 1996, btw.
    • 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.
  • 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
      639
    2. 2
      ATLien_0
      273
    3. 3
      +FloatingFatMan
      172
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!