conquest Posted September 20, 2008 Share Posted September 20, 2008 Having trouble adding to a database, for some reason the values wont insert into the table, here's the code: <?PHP session_start(); $username=$_SESSION['username']; $newcomment = $_POST['comment']; $newsubject = $_POST['subject']; if ($newcomment=="") { echo "Your comment is blank, <a href=generalforum.php>try again</a>"; } else if ($newsubject=="") { echo "Your Subject is blank, <a href=generalforum.php>try again</a>"; } else { $query = "INSERT INTO forumgeneral ('postid', 'comment', 'subject', 'postedby') VALUES (NULL, 'Super Secret', 'Subject Bottom', 'You');"; echo "your comment has been added, <a href=generalforum.php>click here</a> to return to the forums<p>"; echo " Comment: $newcomment, <br> Subject: $newsubject, <br> Posted by: $username"; } ?> I am also getting the following message on my home page after login and dont know what to do, it seems to be only the homepage: <?PHP session_start(); $username=$_SESSION['username']; $newcomment = $_POST['comment']; $newsubject = $_POST['subject']; if ($newcomment=="") { echo "Your comment is blank, <a href=generalforum.php>try again</a>"; } else if ($newsubject=="") { echo "Your Subject is blank, <a href=generalforum.php>try again</a>"; } else { $query = "INSERT INTO forumgeneral ('postid', 'comment', 'subject', 'postedby') VALUES (NULL, 'Super Secret', 'Subject Bottom', 'You');"; echo "your comment has been added, <a href=generalforum.php>click here</a> to return to the forums<p>"; echo " Comment: $newcomment, <br> Subject: $newsubject, <br> Posted by: $username"; } ?> Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/ Share on other sites More sharing options...
adam291086 Posted September 20, 2008 Share Posted September 20, 2008 I dont see where you are doing a database connection and running the variable $query. I think you need to go here www.w3schools.com/php and look into php and mysql Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646445 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 <?php // includes template.conf is where your user details to connect to the db go include("../template/conf.php"); // validate text input fields $website = mysql_escape_string($_POST['website']); $name = mysql_escape_string($_POST['name']); $section = mysql_escape_string($_POST['section']); // check for errors // if none found... // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // generate and execute query $query = "INSERT INTO links (name, website, section) VALUES('$name','$website','$section')"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646459 Share on other sites More sharing options...
conquest Posted September 20, 2008 Author Share Posted September 20, 2008 ??? ok i did what you said dropfaith but now its saying: Error in query: INSERT INTO forumgeneral ('comment', 'subject', 'postedby') VALUES ('','',''). You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''comment', 'subject', 'postedby') VALUES ('','','')' at line 1 Thanks for the help Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646477 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 post your form thats submitting this data Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646483 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2008 Share Posted September 20, 2008 Single-quotes only go around string values, not column names. Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646486 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 crap how did i miss that Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646488 Share on other sites More sharing options...
conquest Posted September 20, 2008 Author Share Posted September 20, 2008 Thanks guys im gettin somewhere now....... but, i have 1 more error. the details are being added but im getting an error because im using a session variable to store the username, heres what ive got. FORM: <form action="addedcomment2.php" method="post"> <table width="600" border="0" cellspacing="20" cellpadding="0"> <tr> <th width="100"><div align="right">Subject:</div></th> <td width="500"><input name="subject" type="text" size="33" maxlength="15"></td> </tr> <tr> <th valign="top"><div align="right">Comment:</div></th> <td><div align="left"> <textarea name="comment" cols="60" rows="8" wrap="soft"></textarea> </div></td> </tr> <tr> <td></td> <td> <div align="right"> <input type="submit" name="Submit" value="Submit"> </div></td> </tr> </table> <p> </p> <p> </p> </form> Script <?PHP session_start(); // includes template.conf is where your user details to connect to the db go include("databasesettings.php"); // validate text input fields $username=$_SESSION['username']; $newcomment = $_POST['comment']; $newsubject = $_POST['subject']; /* $newcomment = mysql_escape_string($_POST['newcomment']); $newsubject = mysql_escape_string($_POST['newsubject']); */ // check for errors // if none found... /* // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); */ // generate and execute query $query = "INSERT INTO forumgeneral (comment, subject, postedby) VALUES ('$newcomment','$newsubject','$username')"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646506 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 whats the error? Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646516 Share on other sites More sharing options...
conquest Posted September 20, 2008 Author Share Posted September 20, 2008 lol sorry, its: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/c/o/n/conquestuk/html/gameforum/addedcomment2.php:1) in /home/content/c/o/n/conquestuk/html/gameforum/addedcomment2.php on line 1 Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646528 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 you have to put the code that starts the session at the very top of the page. Nothing can be sent before it, not even white space. is all i can think of that causes that Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646532 Share on other sites More sharing options...
tradet Posted September 20, 2008 Share Posted September 20, 2008 Header errors You need to have it directly at the top of your page: <?php session_start(); No spaces before <?php and no enter. Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646534 Share on other sites More sharing options...
conquest Posted September 20, 2008 Author Share Posted September 20, 2008 Ah ok, i had Whitespace Fixed now. If your not bored of me already 1 more quick question, how do i save the current time (GMT) to a variable to store to the database? Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646536 Share on other sites More sharing options...
tradet Posted September 20, 2008 Share Posted September 20, 2008 $date = date('Y-m-d G:i:s'); Works for me Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646538 Share on other sites More sharing options...
dropfaith Posted September 20, 2008 Share Posted September 20, 2008 agreed Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646544 Share on other sites More sharing options...
conquest Posted September 20, 2008 Author Share Posted September 20, 2008 thats not GMT time though, how do i correct the time difference: Time given form code: 2008-09-20 11:22:24 Time where i Live: 2008-09-20 19:22:24 Link to comment https://forums.phpfreaks.com/topic/125083-adding-to-a-database/#findComment-646552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.