• 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

    • Glad they aren't hiking the price $10. If anything they should offer a $10 discount to anyone that provides a receipt for that awful movie.
    • At this point, with this - they have proven it to be a gimmick to “look like Windows”, rather than “here’s a nice useable OS”
    • AMD releases new Windows 10 and 11 chipset driver for Ryzen processors by Taras Buria Following a major firmware update for a TPM-Pluton security flaw, AMD has a new chipset driver for all of its supported Ryzen platforms and processors. Version 7.06.02.123 is now available for download, and it adds unnamed bug fixes and an important addition to the AMD PPM Provisioning File driver, which improves the security of the component. The security update in question is CETCOMPAT support for the AMD PPM Provisioning File driver. CET or Control-flow Enforcement Technology (CET) was introduced for Windows 10 versions 20H1 and 20H2 in early 2021. In a nutshell, it is an Intel-developed protection system that prevents malware from changing memory addresses (Shadow Stack) or redirecting programs to malicious code by ensuring they use only approved, safe locations (Indirect Branch Tracking). AMD brought CETCOMPACT support to parts of its Ryzen chipset driver suite (the driver you download and install is a set of different drivers serving different purposes that AMD lets you select on the first screen) in October 2024. Now, with driver 7.06.02.123, CET compatibility has been added to the AMD PPM Provisioning File driver, which is responsible for core parking and power management. Here is the list of updated drivers in today's release: Driver Version (Windows 10) Version (Windows 11) Changelog AMD PPM Provisioning File Driver 8.0.0.53 8.0.0.53 CETCOMPAT support is added AMD PMF-6000 Series Driver 24.0.6.0 24.0.6.0 Bug fixes AMD S0i3 Filter Driver 4.5.0.1020 4.5.0.1020 AMD 3D V-Cache Performance Optimizer Driver 1.0.0.11 1.0.0.11 AMD PMF-7040 Series Driver 24.2.6.0 24.2.6.0 AMD PMF-8000Series Driver 25.5.25.0 25.5.25.0 AMD PMF Ryzen AI 300 Series Driver 1 Not Applicable 25.6.28.0 AMD's latest Ryzen chipset driver supports 64-bit Windows 10 and 11 systems with the following motherboards: A-Series B-Series X-Series Threadripper AMD A320 AMD A520 AMD B350 AMD B450 AMD B550 AMD B650 / B650E AMD X370 AMD X470 AMD X570 AMD X670 / X670E AMD X870 / X870E AMD TRX40 AMD TRX50 AMD WRX80 AMD WRX90 You can download AMD Ryzen Chipset Driver 7.0.6.02.123 from the official support website. Full release notes and additional information is available on the same page.
    • I never got to experience BOB Hello even now because gaming laptops I bought always omitted the IR camera. And the standalone cameras for BOB Hello are super expensive
    • I wonder what $99 Chinese cellphone this phone will be based on. Whoever is dumb enough to buy the phone and/or get this service deserves everything they get.
  • 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
      644
    2. 2
      ATLien_0
      274
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      137
  • Tell a friend

    Love Neowin? Tell a friend!