user1839483
user1839483

Reputation: 123

HTML table inputting data to a mysql database

I am trying to get a form to input data into my "mysql database" however i am getting an error message and also it is inputting a blank data everytime the page loads.

Here is my code:

    <form action="insert.php" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>

<?php
// This is the connection to my database
$con = mysql_connect('127.0.0.1', 'shane', 'diamond89');
if (!$con){
die('Could not Connect: ' . mysql_error());
}

// This creates my table layout
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Delete</th>
</tr>";

// This selects which database i want to connect to
$selected = mysql_select_db("shane",$con);
if (!$con){
die("Could not select examples");
}

// This inserts new information to the Database
$query = "INSERT INTO test1 VALUES('id', '$name')";

$result = mysql_query($query);
if ($result){
echo("Input data is Successful");
}else{
echo("Input data failed");
}

// This chooses which results i want to select from
$result = mysql_query("SELECT `id`, `name` FROM `test1` WHERE 1");


// This outputs the information into my table
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "[D]" . "</td>";
echo "</tr>";
}
echo "</table>";

// This closes my connection
mysql_close($con);

?>

Here is the error message:

( ! ) SCREAM: Error suppression ignored for ( ! ) Notice: Undefined variable: name in C:\wamp\www\sql_table.php on line 36 Call Stack

Time Memory Function Location

1 0.0006 250360 {main}( ) ..\sql_table.php:0

Upvotes: 0

Views: 185

Answers (1)

Alarid
Alarid

Reputation: 818

You are trying to access to the POST data, so you should do something like that :

EDIT: be careful about the data you put into your database. You should use a modern database API, or, at least, escape your data (cf bellow code)

<form action="insert.php" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>

<?php
// Following code will be called if you submit your form
if (!empty($_POST['name'])) :

// This is the connection to my database
$con = mysql_connect('127.0.0.1', 'shane', 'diamond89');
if (!$con){
die('Could not Connect: ' . mysql_error());
}

// This creates my table layout
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Delete</th>
</tr>";

// This selects which database i want to connect to
$selected = mysql_select_db("shane",$con);
if (!$con){
die("Could not select examples");
}

// This inserts new information to the Database
$query = "INSERT INTO test1 VALUES('id', \'".mysql_real_escape_string($_POST['name'])."\')";

$result = mysql_query($query);
if ($result){
echo("Input data is Successful");
}else{
echo("Input data failed");
}

// This chooses which results i want to select from
$result = mysql_query("SELECT `id`, `name` FROM `test1` WHERE 1");


// This outputs the information into my table
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "[D]" . "</td>";
echo "</tr>";
}
echo "</table>";

// This closes my connection
mysql_close($con);

endif;
?>

Upvotes: 1

Related Questions