Lawrence Smith
Lawrence Smith

Reputation: 337

Data not inserting

I have created a simple web form in which you input data into and then it is posted into the database, when submitting the information it is submitted correctly but when I view it in the table the data is invisible.

query

CREATE TABLE `recipe` (
    `id` int(4) NOT NULL auto_increment,
    `recipename` varchar(65) NOT NULL default '',
    `ingredients` varchar(65) NOT NULL default '',
    `instructions` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=0 ; 

php code

<?php

$host="localhost"; // Host name
$username="my username"; // Left empty due to privacy
$password="mypassword"; // Left empty due to privacy
$db_name="mydatabase"; // Left empty due to privacy
$tbl_name="recipe"; // Table name


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];


$sql="INSERT INTO $tbl_name(recipename, ingredients, instructions)VALUES('$recipe', '$ingredients', '$instructions')";
$result=mysql_query($sql);


if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='recipe.php'>Back to main page</a>";
} else {
   echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?> 

Web Form

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr>
    <td>
      <form name="form1" method="post" action="insert_ac.php">
        <table width="100%" border="0" cellspacing="1" cellpadding="3">
          <tr>
            <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
          </tr>
          <tr>
            <td width="71">Recipe name</td>
            <td width="6">:</td>
            <td width="301"><input name="name" type="text" id="recipe"></td>
          </tr>
          <tr>
            <td>Ingredients</td>
            <td>:</td>
            <td><input name="lastname" type="text" id="ingredients"></td>
          </tr>
          <tr>
            <td>Instructions</td>
            <td>:</td>
            <td><input name="email" type="text" id="instructions"></td>
          </tr>
          <tr>
            <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>

I would love to know if anyone knows the reason the data is invisible.enter image description here

Image of table with no data

Upvotes: 1

Views: 83

Answers (1)

kittycat
kittycat

Reputation: 15045

$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];

needs to be:

$recipe=$_POST['name'];
$ingredients=$_POST['lastname'];
$instructions=$_POST['email'];

You have the form field names wrong, so it is not getting a value and thus inserting empty data. If you set error_reporting to E_ALL you would have gotten an error. Make sure you develop with it at that setting.

Also the name attribute in the form field is what the index name would be for $_POST['INDEX_NAME'], you are mistakingly using the id attribute for that.

Upvotes: 4

Related Questions