Jump to content

PHP to Mysql Update Help


zour1el

Recommended Posts

I have a few HTML and PHP pages that does a circle....

Basically....

 

Html form to php that inserts into mysql

then PHP form that searches the mysql for a record and then outputs via echo

 

I want another text box on this page that will use that record criteria and allow an update to the record.... any help please on what is best case practice before I start this last piece

Link to comment
Share on other sites

You want an update form correct?

 

Usually, I would simply grab all the data from the mysql that I am going to update, get its current values in the SQL table and populate the input fields respectively. Then when the form submits, I update the SQL table based on the changes..

 

$field1 would from the FORM.. or in your case the text box.

UPDATE `table_name` SET `field1` = '$field1' WHERE `field_id`= '$id'

 

Its the simplest way.

Link to comment
Share on other sites

sorry for the delay thanks.....

 

 

I went back to the code and the database and found something odd......

 

 

It was adding a blank record as well....

 

so i went basic and grabbed a cheesy 101 form and php and created a new DB and it was also adding a blank record...

 

here are the 2 pages

 

<html>
<body>

<form action="duh.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

 

and

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("final", $con);

$sql="INSERT INTO my_db (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

Link to comment
Share on other sites

this is most likely because you are not first checking to see if the form has been submitted.. you will want to add the name attribute to the input type submit as well...for this example we will say that the name is name='submit'...

 

<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("final", $con);

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$submit = $_POST['submit'];
if(isset($submit)){
if(!empty($firstname) && !empty($lastname) && !empty($age)){
$sql="INSERT INTO my_db (FirstName, LastName, Age)
VALUES
('$firstname',$lastname',$age')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }else{
echo "1 record added";
}
}
}

mysql_close($con)
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.