php - When i submit from the HTML page I receive the following error: "Error: Table 'searchquery_interfaithmedical_com.Persons' doesn't exist" -
i having bit of difficulty setting sql queries php.
i have next php script:
<?php $con = mysql_connect("url","username","password"); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("searchquery_interfaithmedical_com", $con); $sql = "create table persons ( firstname varchar(15), lastname varchar(15), age int )"; $sql="insert persons (firstname, lastname, age) values ('$_post[firstname]','$_post[lastname]','$_post[age]')"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } echo "1 record added"; mysql_query($sql); $sql = "select * persons"; mysql_close($con); ?>
what looking take first name, lastly name , age table persons in searchquery_interfaithmedical_com database. when submit html page receive next error: "error: table 'searchquery_interfaithmedical_com.persons' doesn't exist".
isn't php script taking care of creating of table before inserting? how right issue?
you're overwriting $sql
variable create table before run it. try:
$sql = "create table persons ( firstname varchar(15), lastname varchar(15), age int )"; mysql_query($sql); $sql = "insert persons (firstname, lastname, age) values ('$_post[firstname]','$_post[lastname]','$_post[age]')";
to fetch values table you'd this:
$sql = mysql_query("select * persons"); while($results = mysql_fetch_array($sql)) { echo $results['firstname'] . ', ' . $results['lastname'] . ', ' . $results['age'] . '<br/>'; }
php html sql
No comments:
Post a Comment