php - HTML table inputting data to a mysql database -
i trying form input data "mysql database" getting error message , inputting blank data everytime page loads.
here code:
<form action="insert.php" method="post"> name: <input type="text" name="name"> <input type="submit" value="submit"> </form> <?php // connection database $con = mysql_connect('127.0.0.1', 'shane', 'diamond89'); if (!$con){ die('could not connect: ' . mysql_error()); } // creates table layout echo "<table border='1'> <tr> <th>id</th> <th>name</th> <th>delete</th> </tr>"; // selects database want connect $selected = mysql_select_db("shane",$con); if (!$con){ die("could not select examples"); } // inserts new information database $query = "insert test1 values('id', '$name')"; $result = mysql_query($query); if ($result){ echo("input data successful"); }else{ echo("input data failed"); } // chooses results want select $result = mysql_query("select `id`, `name` `test1` 1"); // outputs information 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>"; // closes connection mysql_close($con); ?>
here error message:
( ! ) scream: error suppression ignored ( ! ) 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
you trying access post data, should :
edit: careful data put database. should use modern database api, or, @ least, escape 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 called if submit form if (!empty($_post['name'])) : // connection database $con = mysql_connect('127.0.0.1', 'shane', 'diamond89'); if (!$con){ die('could not connect: ' . mysql_error()); } // creates table layout echo "<table border='1'> <tr> <th>id</th> <th>name</th> <th>delete</th> </tr>"; // selects database want connect $selected = mysql_select_db("shane",$con); if (!$con){ die("could not select examples"); } // inserts new information database $query = "insert test1 values('id', \'".mysql_real_escape_string($_post['name'])."\')"; $result = mysql_query($query); if ($result){ echo("input data successful"); }else{ echo("input data failed"); } // chooses results want select $result = mysql_query("select `id`, `name` `test1` 1"); // outputs information 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>"; // closes connection mysql_close($con); endif; ?>
Comments
Post a Comment