gibigbig Posted June 28, 2007 Share Posted June 28, 2007 <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 <?php // connect to database localhost with username peter and password abc123 $con = mysql_connect("localhost","peter","abc123"); // if the connection did not take do inside the if. if (!$con) { // kill the script as a db connection could not be made die('Could not connect: ' . mysql_error()); } // select the database (my_db) we wish to use with our db connection mysql_select_db("my_db", $con); // insertt data into the database using POST data from a form. $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; // if the query returns false for $sql than die and print out the error if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } // print that everything went good. echo "1 record added"; // close the connection. mysql_close($con) ?> Quote Link to comment Share on other sites More sharing options...
gibigbig Posted June 28, 2007 Author Share Posted June 28, 2007 ok since the script hasnt selcted a table in the database to input the data in, where does the information go then? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 It will throw an error if no database is selected. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 You would use the function mysql_select_db() to select the database. Here is where you would put it in the script: EDIT: Oops, didn't read the question right Quote Link to comment Share on other sites More sharing options...
gibigbig Posted June 28, 2007 Author Share Posted June 28, 2007 no, it did select the database, just not the table. how does is the information stored if no TABLE in the database is specified inthe script? tnx Quote Link to comment Share on other sites More sharing options...
biffyboy Posted June 28, 2007 Share Posted June 28, 2007 The table is selected in the query ($sql) - "person" would be your table. <?php //INSERT INTO [tableName]=person $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted June 28, 2007 Share Posted June 28, 2007 You did specify a table $sql="INSERT INTO person INSERT INTO table_name (col1, col2) VALUES ('val1', 'val2'); Quote Link to comment Share on other sites More sharing options...
gibigbig Posted June 28, 2007 Author Share Posted June 28, 2007 o, so it IS specified, tnx alot Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 Look at this line right here: <?php $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; ?> It does specify a table. INSERT INTO person "person" is the name of the table that you are storing the information in. EDIT: Geez your fast Frost, haha. Quote Link to comment Share on other sites More sharing options...
cluce Posted June 28, 2007 Share Posted June 28, 2007 sql="INSERT INTO person (FirstName, LastName, Age) VALUES you sql says their is a table called "person" you need to add a table in your database called person for this to work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.