Jump to content

Insert Into DB based on id


forumnz

Recommended Posts

Each thing in the db has its own id and I want a user to be able to edit what they choose to. Here is my code. Before I had the id and $nm it just added a new row but since i added the above, it decided not to work:

 

<html>

<body>



<?php



if ($submit) {

  // process form

  $db = mysql_connect("localhost", "designervi_mag", "***");

  mysql_select_db("designervi_mag",$db);

  $sql = "INSERT INTO mag (stg1,stg2,stg3) VALUES ('$stg1','$stg2','$stg3') WHERE id='$nm'";

  $result = mysql_query($sql);

  echo "Thank you! Information entered.\n";

} else{



  // display form



  ?>



  <form method="post" action="<?php echo $PHP_SELF?>">
  
  1:<input name="nm" type="checkbox" value=""><br />

  Link<input type="Text" name="stg1"><br>

  Image<input type="Text" name="stg2"><br>

  Text<input type="Text" name="stg3"><br>

  <input type="Submit" name="submit" value="Enter information">

  </form>



  <?php



} // end if



?>



</body>



</html>

Link to comment
https://forums.phpfreaks.com/topic/49766-insert-into-db-based-on-id/
Share on other sites

You're not catching the mysql_error() in there. Try that to see what the error is. Also try echoing $sql to check it's ok.

 

Another favourite of mine when debugging is putting this right at the top (just after <?php)

 

error_reporting(E_ALL);

ini_set('display_errors', '1');

 

If you can't resolve the issue using the errors, post them back in here...

Hi all, thanks DaveEverFade.

Ive tried that but it came up with:

 

Notice: Undefined variable: nm in /home/designervi/domains/designervision.co.nz/public_html/mag/i.php on line 22

UPDATE mag (stg1,stg2,stg3) VALUES ('..','..','..') WHERE id=Thank you! Information entered.

 

 

mmarif4u: Update for current user

Thanks, i got that a min ago. Heres my new code - still doesn't work. Help?

 

<html>

<body>



<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');



if ($submit) {

  // process form

  $db = mysql_connect("localhost", "designervi_mag", "  ");

  mysql_select_db("designervi_mag",$db);

  $sql = "UPDATE mag (stg1,stg2,stg3) VALUES ('$stg1','$stg2','$stg3') WHERE id=$nm";

  $result = mysql_query($sql);
  
  echo $sql;

  echo "Thank you! Information entered.\n";

} else{



  // display form



  ?>



  <form method="post" action="<?php echo $PHP_SELF?>">
  
  1:<input name="$nm" type="checkbox" value=""><br />

  Link<input type="Text" name="stg1"><br>

  Image<input type="Text" name="stg2"><br>

  Text<input type="Text" name="stg3"><br>

  <input type="Submit" name="submit" value="Enter information">

  </form>



  <?php



} // end if



?>



</body>



</html>

you have do define $nm.

 

I can only assume you get the id from the url - if this is the case - add this to the top of your script:

 

<?PHP
if (is_numeric($_REQUEST['id'])) { // check and see that the id is numeric
   $nm = $_REQUEST['id'];
   //put the rest of your code here
} else { //if the ID is not numeric - its probably not valid - so add this at the end of the if statement that holds the rest of your code.
  echo '<font color="red" size="+1">You have entered an incorrect ID, please go back and try again</font>';
  exit();
}
?>

 

UPDATE mag set stg1='',stg2='',stg3='' WHERE id=''Thank you! Information entered.

 

Look at the stg1='' valus its empty it mean that ur data is not coming from form ok do it like this:

 

$stg1=$_POST['stg1'];
$stg1=$_POST['stg1'];
$stg1=$_POST['stg1'];

$sql="UPDATE mag set stg1='$stg1',stg2='stg2',stg3='stg3' WHERE id='$nm'";

forumz,

 

I answered you in my first post.

 

you are not defining the variable $nm

 

you need to define that variable before this will work, and that variable needs to correlate to an ID that is pulled from the database.

 

In your code your defining nm, by using a checkbox - why I do not know, but the only 2 outputs you will ever get from a checkbox is either "on" or "off", meaning your query is never going to work.

 

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.