Jump to content

Recommended Posts

Ok to get straight to the point.

I have tried multiple ways to create a form that will add data to a mysql table.  I know I got the coding right, but it will still not add to the database.  It's like its not letting the form insert data to the table.  Any help is appreciated

thanks

 

**I'm new to php**

Link to comment
https://forums.phpfreaks.com/topic/85107-write-to-mysql-using-php-issue/
Share on other sites

here is a form im trying to use

 

 

 

<html>

<body>



<?php



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

mysql_select_db("mydb",$db);



if ($submit) {

  // here if no ID then adding else we're editing

  if ($id) {

    $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";

  } else {

    $sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";

  }

  // run SQL against the DB

  $result = mysql_query($sql);

  echo "Record updated/edited!<p>";

} elseif ($delete) {

// delete a record

    $sql = "DELETE FROM employees WHERE id=$id";	

    $result = mysql_query($sql);

    echo "$sql Record deleted!<p>";

} else {

  // this part happens if we don't press submit

  if (!$id) {

    // print the list if there is not editing

    $result = mysql_query("SELECT * FROM employees",$db);

    while ($myrow = mysql_fetch_array($result)) {

      printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

  printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

    }

  }



  ?>

  <P>

  <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

  <P>

  <form method="post" action="<?php echo $PHP_SELF?>">

  <?php



  if ($id) {

    // editing so select a record

    $sql = "SELECT * FROM employees WHERE id=$id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);

    $id = $myrow["id"];

    $first = $myrow["first"];

    $last = $myrow["last"];

    $address = $myrow["address"];

    $position = $myrow["position"];

    // print the id for editing



    ?>

    <input type=hidden name="id" value="<?php echo $id ?>">

    <?php

  }



  ?>

  First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>

  Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>

  Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>

  Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>

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

  </form>



<?php



}



?>



</body>

</html>

You are assuming that register_globals is enabled. It is disabled on most hosts these days because it can create security problems when enabled.

 

Change:

<?php
if ($submit) {
  // here if no ID then adding else we're editing
  if ($id) {
    $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";
?>

to

<?php
if (isset($_POST['submit'])) {
  // here if no ID then adding else we're editing
  if ($_POST['id'] != '') {
    $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";
?>

 

There are other changes to be made also. Where ever you reference a plain variable that you're where you're expecting the value to come from the form, you need to get the value from the $_POST superglobal array like I did above.

 

Ken

to be completely honest and truthful, I have no idea what that means exactly.  I'll mess around with it as best I can.  It now submits the id number to the database, but does not submit the name or other info.  If you are willing to advise me more it would be greatly appreciated.  I've been wanting to try php for a while because it has many applications.  I'm modifying this page to make it into a song request submission page for a web radio station.  I apologize for causing trouble.

 

register_globals is on

and also how do i change this line to avoid errors.

= "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";

 

Try to enable the mysql error reporting.Then hopefully u will know what the errors are...

 

$result = mysql_query($sql) or die (mysql_error());

Put this line in every query.

 

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.