Hellusius Posted September 2, 2006 Share Posted September 2, 2006 I made a database with a few things, just to take a peak at how things work, but I already stranded, I made a database, which I connected to and I made a few forms so you can insert a row into the database, this is the coding[code]<form action = "/postit.html" method = "post">Enter your name: <br /><input type = "text" size = "40" name = "name" /><br />Enter your age: <br /><input type = "text" size = "3" name = "age" /><br />Enter your sex: <br /><input type = "text" size = "1" name = "sex" /><br /><br /><input type = "submit" value = "Add Data" /></form> <?php// collect data sent from form$name = $_POST['name'];$age = $_POST['age'];$sex = $_POST['sex'];// connection variables$host = "*****"; // almost inavariably$db_name = "*****";$db_user = "*****";$db_pass = "*****";$db_table = "details";// connect to host and select dbmysql_connect($host, $db_user, $db_pass);mysql_select_db($db_name);// create and execute the query to insert data$query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')";$result = mysql_query($query);?>[/code]Did I forget something, cause nothing is being added to the database in my oppinion Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/ Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 Similar but notice how the flow of the script goes. (btw - don't post your real db details. I edited them for you).[code]<?phpif (isset($_POST['submit'])) { // collect data sent from form but ONLY if it was submitted $name = $_POST['name']; $age = $_POST['age']; $sex = $_POST['sex']; // connection variables $host = "*****"; // almost inavariably $db_name = "details"; $db_user = "*****"; $db_pass = "*****"; $db_table = "details"; // connect to host and select db mysql_connect($host, $db_user, $db_pass); mysql_select_db($db_name); // create and execute the query to insert data $query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')"; $result = mysql_query($query);} else {?><form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">Enter your name: <br /><input type = "text" size = "40" name = "name" /><br />Enter your age: <br /><input type = "text" size = "3" name = "age" /><br />Enter your sex: <br /><input type = "text" size = "1" name = "sex" /><br /><br /><input type = "submit" name = "submit" value = "Add Data" /></form> <?php}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84914 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 Oops :-[, sorry I did that, thank you for editing it, going to try the code now.P.S, hope you don't mind me asking, but what is the thing<?php}?>on the bottom for? Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84917 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 It closes off the loop that begins with else { just ahead of the form.The logic flow is basically this:IF the submit button was pressed { process the data and stick it in the database } else {show me the form} Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84924 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 ok that made sense, starting to understand.anyway, I get a blanco page, but how can I see if anything is added to the database? Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84925 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 Change:[code] $result = mysql_query($query);} else {[/code]To:[code] $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // in case it bombs echo "Thanks. The information was added." // or whatever else you want to do } else {[/code]You'll need to use whatever tools you usually need to view the database to see if (that) the data were added. Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84930 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 bumped into a new problem now[code]<?phpif (isset($_POST['submit'])) { // collect data sent from form but ONLY if it was submitted $name = $_POST['name']; $age = $_POST['age']; $sex = $_POST['sex']; // connection variables $host = "******"; // almost inavariably $db_name = "details"; $db_user = "******"; $db_pass = "******"; $db_table = "details"; // connect to host and select db mysql_connect($host, $db_user, $db_pass); mysql_select_db($db_name); // create and execute the query to insert data $query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')"; $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // in case it bombs?><center><table width="500" bgcolor="000000" cellspacing="0" cellpadding="0" align="center"> <tr> <td><font color="1055AA" size="3">Infromation has been added, <a href="/index.php">click here</a> to return to the homepage</font> </td> </tr></table></center><?php} else {?><form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">Enter your name: <br /><input type = "text" size = "40" name = "name" /><br />Enter your age: <br /><input type = "text" size = "3" name = "age" /><br />Enter your sex: <br /><input type = "text" size = "1" name = "sex" /><br /><br /><input type = "submit" name = "submit" value = "Add Data" /></form> <?php}?>[/code]it gave me this error[quote]Error: No database selected with query INSERT INTO details (id, name, age, sex) VALUES ('', 'test' , '00' , 'v')[/quote]I know its something with that line, but I can't see what, any help is very much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84943 Share on other sites More sharing options...
hackerkts Posted September 3, 2006 Share Posted September 3, 2006 Check again your database 'details' is it there ? Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84953 Share on other sites More sharing options...
eRott Posted September 3, 2006 Share Posted September 3, 2006 Try this:[code]<html><head><title>Test</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><?if(isset($_POST['add'])){$dbhost = '$dbhost';$dbuser = '$dbuser';$dbpass = '$dbpass';$dbname = '$dbname';$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());$db = mysql_select_db($dbname, $conn) or die(mysql_error());$name = $_POST['name'];$age = $_POST['age'];$sex = $_POST['sex'];$query = "INSERT INTO information (name, age, sex) VALUES ('$name', '$age', '$sex')";mysql_query($query) or die('Error, insert query failed');mysql_close($conn);echo "New entry added!";}else{?><form method="post"><table width="400" border="0" cellspacing="1" cellpadding="2"><tr> <td width="100">Name</td><td><input name="name" type="text" id="name"></td></tr><tr> <td width="100">Age</td><td><input name="age" type="text" id="age"></td></tr><tr> <td width="100">Sex</td><td><input name="sex" type="text" id="sex" size = "1"></td></tr><tr> <td width="100"> </td><td> </td></tr><tr> <td width="100"> </td><td><input name="add" type="submit" id="add" value="Add Data"></td></tr></table></form><?}?></body></html>[/code]As for the database, use this to create it:create_db.php[code]<?php$dbhost = '$dbhost';$dbuser = '$dbuser';$dbpass = '$dbpass';$dbname = '$dbname';$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());$db = mysql_select_db($dbname, $conn) or die(mysql_error());$query = 'CREATE DATABASE contacts';$result = mysql_query($query);mysql_select_db('contacts') or die('Cannot select database');$query = 'CREATE TABLE information( '. 'id INT NOT NULL AUTO_INCREMENT, '. 'name VARCHAR(50) NOT NULL, '. 'age VARCHAR(3) NOT NULL, '. 'sex VARCHAR(6) NOT NULL, '. 'PRIMARY KEY(id))';$result = mysql_query($query);mysql_close($conn);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84970 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 The query you had is fine. The error it displays suggests that there was a problem establishing a real connection. Usually a database name is something like username_databasenameyouchose rather than just the databasenameyouchose. So carefully check the values you're using when you define the database conn variables (which you shouldn't post here but obviously you need in your working script on your server).Let's take the error trapping a little further. Adjust the connection lines in the script to these:[code]// connect to host and select db mysql_connect($host, $db_user, $db_pass) of die("Error connecting: ". mysql_error()); // any problems? mysql_select_db($db_name) or die(" Error selecting database: ". mysql_error()); // any problems?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84975 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 now I get an error on the line[code]mysql_select_db($db_name) or die(" Error selecting database: ". mysql_error()); // any problems?[/code]which is[code]Parse error: syntax error, unexpected T_STRING in /home/www/warnicro.awardspace.com/postit.html on line 16[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84977 Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 The syntax error message relates to the line where the php interpreter found something it couldn't handle not necessarily that the error exists on the line number it gives. Carefully check to be sure that quotes are matched in all the preceding lines, and that statements all end properly, i.e. you don't have $db_name = "details ... without the closing quote or the closing ; Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84982 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 think I got it, you wrote "of" instead of "or"Edit: got some result :D[code]Rows 1Row length ø 20Row size ø 2,068 BytesNext Autoindex 2Creation Sep 02, 2006 at 12:49 AMLast update Sep 02, 2006 at 10:00 PMLast check Sep 02, 2006 at 06:49 PM[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19528-made-database-but-add-does-not-work/#findComment-84996 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.