Jump to content

Adding to a Database


conquest

Recommended Posts

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

<?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

??? 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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.