Jump to content

Updating Information in a MySQL Database Using PHP. Please help! Haha :)


Transmission94

Recommended Posts

Hi there, I have appropriated a script from http://www.spoono.com/php/tutorials/tutorial.php?id=23 for my own needs, but it is not working out for me. I have 3 PHP pages all in the same directory (Please not that the comments were not all written by me and thus might not be properly describing what is happening):

 

1.php:

<?php 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","admin","password"); 

//select which database you want to edit
mysql_select_db("shop"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the shit
   $result = mysql_query("select * from product order by prod_id") or die(mysql_error()); 
   
   //run the while loop that grabs all the products
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $prod_name=$r["prod_name"];//take out the product name
      $prod_id=$r["prod_id"];//take out the id
     
 //make the title a link
      echo "<a href='2.php?cmd=edit&prod_id=$prod_id'>$prod_name - Edit</a>";
      echo "<br>";
    }
}
?>

 

 

 

2.php:

<?php

mysql_connect("localhost","admin","password") or die ("no connection"); 

//select which database you want to edit
mysql_select_db("shop") or die ("no shop");


if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $prod_id = $_GET["prod_id"];
      $sql = "SELECT * FROM product WHERE prod_id=$prod_id";
      $result = mysql_query($sql) or die ("Oops");        
      $myrow = mysql_fetch_array($result); 
   }
      ?>
  
      <form action="3.php" method="post">
      <input type=hidden name="prod_id" value="<?php echo $myrow["prod_id"] ?>">
   
      Product Name:<INPUT TYPE="TEXT" NAME="prod_name" VALUE="<?php echo $myrow["prod_name"] ?>" SIZE=30><br>
      Description:<TEXTAREA NAME="prod_description" ROWS=10 COLS=30><?php echo $myrow["prod_description"] ?></TEXTAREA><br>
      Price:<INPUT TYPE="TEXT" NAME="prod_price" VALUE="<?php echo $myrow["prod_price"] ?>" SIZE=30><br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
   <?php } ?>

 

 

3.php

<?php
   if ($_POST["$submit"])
   {
      $prod_name = $_POST["prod_name"];
      $prod_price = $_POST["prod_price"];
  $prod_description = $_POST["prod_description"];

      $sql = "UPDATE product SET prod_name='$prod_name',prod_price='$prod_price',prod_description='$prod_description' WHERE prod_id=$prod_id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
   }
else die("<h1>FOR THE LOVE OF GOD, WORK!</h1>");
?>

 

 

Basically, the first two pages do what they are supposed to, but in 3.php the condition of ($_POST["$submit"]) is not met, and thus the code underneath it isn't executed. I don't have a good enough understanding to know why the condition is not being met.

 

Any help is very much appreciated!

 

 

Thanks,

 

 

 

Transmission94

Hi mrMarcus! :)

 

Thanks for the input, but when I do as you say I get the error...

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\xampp\htdocs\3.php on line 19

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\xampp\htdocs\3.php on line 19
Thank you! Information updated. 

... instead of my own error message.

now yo have to include your database connection to 3.php and you will be set.

 

your best bet is to take this:

 

<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","admin","password"); 
   
//select which database you want to edit
mysql_select_db("shop");
?>

 

and put it in a file called connect.php (or whatever you like), and include it within it page thereafter that requires a database connection.

 

so, for 2.php, you would replace:

 

mysql_connect("localhost","admin","password") or die ("no connection"); 
   
//select which database you want to edit
mysql_select_db("shop") or die ("no shop");

 

with:

 

include ('/path/to/connect.php'); //path/to/ of course being the path to your file .. if it resides within the same folder for test purposes, just have connect.php with no path;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.