Jump to content

[SOLVED] Not Inserting into Database


Akenatehm

Recommended Posts

Hey Guys,

 

The following script displays no errors and carries on as if it was succesful but it is not inserting into the database.

 

I have spent hours trying to figure this out. Help would be greatly appreciated.

 

<?php
$host = 'host';
$username = 'username';
$pass = 'pass';
$db = 'database';
$con = mysql_connect($host,$username,$pass);
if (!con)
{
	die('Could not Connect:' . mysql_error());
}

mysql_select_db("news");

$poster = $_COOKIE['user'];
$date = date("F j,Y");
$content = $_POST['entry'];

if($_POST['type'] == 'mainsite'){
mysql_query("INSERT INTO mainsite (poster, date, content) VALUES($poster,$date,$content)");
 header("Location: succesfulpost.php");
}

elseif($_POST['type'] == 'adminsite'){
mysql_query("INSERT INTO adminsite (poster, date, content) VALUES($poster,$date,$content)");
header("Location: succesfulpost.php");
}

else{
echo "You have encountered an error.";
}
?>

Link to comment
Share on other sites

if($_POST['type'] == 'mainsite'){ << is this from a url if so you need to use $_GET[''] or

is it in a hidden form field.

 

Sorry, should have explained it better.

 

I have a drop down menu in a form. The two options are mainsite and adminsite. If they select mainsite, i want to add it to the mainsite table and vice versa for adminsite.

Link to comment
Share on other sites

try this please.

 

i have not added no database protection yet but see if it works.

<?php
$host = 'host';
$username = 'username';
$pass = 'pass';
$db = 'database';
$con = mysql_connect($host,$username,$pass);
if (!$con)
   {
      die('Could not Connect:' . mysql_error());
   }
   
$res=mysql_select_db("news",$db);

$poster = $_COOKIE['user'];
$date = date("F j,Y");
$content = $_POST['entry'];

if($_POST['type'] == 'mainsite'){
   
$sql="INSERT INTO mainsite (poster, date, content) VALUES($poster,$date,$content)";
   
   $res=mysql/-query($sql)or die(mysql_error()); 

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
$sql1="INSERT INTO adminsite (poster, date, content) VALUES($poster,$date,$content)";
      
   $res=mysql_query($sql)or die(mysql_error()); 
      
   header("Location: succesfulpost.php");
   
}else{
   echo "You have encountered an error.";
}
?>

Link to comment
Share on other sites

Ok. I added the $ to con. I get the following error:

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/tedolinar/public_html/adminpanel/news/addentryscript.php on line 12

Fatal error: Call to undefined function query() in /home/tedolinar/public_html/adminpanel/news/addentryscript.php on line 22

Link to comment
Share on other sites

try this a bit upgraded

 

edited and corrected the $sql1

 

should work now.

<?php
$host = 'host';
$username = 'username';
$pass = 'pass';
$db = 'database';
$con = mysql_connect($host,$username,$pass);
if (!$con)
   {
      die('Could not Connect:' . mysql_error());
   }
   
$res=mysql_select_db("news",$db);

$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$content = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
$sql="INSERT INTO mainsite (poster, date, content) VALUES($poster,$date,$content)";
   
   $res=mysql_query($sql)or die(mysql_error()); 

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
$sql1="INSERT INTO adminsite (poster, date, content) VALUES($poster,$date,$content)";
      
   $res=mysql_query($sql1)or die(mysql_error()); 
      
   header("Location: succesfulpost.php");
   
}

if(!$_POST['type'] == 'adminsite' && !$_POST['type'] == 'mainsite'){
   echo "You have encountered an error, Sorry please try agin!.";
}

?>

Link to comment
Share on other sites

use the database connection like this then.

 

<?php

$db=mysql_connect("localhost","username","password");
$res_db=mysql_select_db("news",$db)or die(mysql_error());

$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$content = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
$sql="INSERT INTO mainsite (poster, date, content) VALUES($poster,$date,$content)";
   
   $res=mysql_query($sql)or die(mysql_error()); 

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
$sql1="INSERT INTO adminsite (poster, date, content) VALUES($poster,$date,$content)";
      
   $res=mysql_query($sql)or die(mysql_error()); 
      
   header("Location: succesfulpost.php");
   
}

if(!$_POST['type'] == 'adminsite' && !$_POST['type'] == 'mainsite'){
   echo "You have encountered an error, Sorry please try agin!.";
}

?>

Link to comment
Share on other sites

sorry no single ' quotes around the insert variables

<?php

$db=mysql_connect("localhost","username","password");
$res_db=mysql_select_db("news",$db)or die(mysql_error());

$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$content = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (poster, date, content) VALUES('$poster','$date','$content')";
   
   $res=mysql_query($sql)or die(mysql_error()); 

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (poster, date, content) VALUES('$poster','$date','$content')";
      
   $res=mysql_query($sql)or die(mysql_error()); 
      
   header("Location: succesfulpost.php");
   
}

if(!$_POST['type'] == 'adminsite' && !$_POST['type'] == 'mainsite'){
   echo "You have encountered an error, Sorry please try agin!.";
}

?>

Link to comment
Share on other sites

Ok. Its inserting fine now. But some of the fields were in the wrong place so i moved it around, now only the date field is showing.

 

Here's the code:

$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$content = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (entry, poster, date) VALUES('$entry','$poster','$date')";
   
   $res=mysql_query($sql)or die(mysql_error()); 

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (entry, poster, date) VALUES('$entry','$poster','$date')";
      
   $res=mysql_query($sql)or die(mysql_error()); 
      
   header("Location: succesfulpost.php");
   
}

Link to comment
Share on other sites

well if that the order

 

try now m8

<?php

$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$content = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (content, poster, date) VALUES('$entry','$poster','$date')";
   
   $res=mysql_query($sql)or die(mysql_error());

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (content, poster, date) VALUES('$entry','$poster','$date')";
     
   $res=mysql_query($sql1)or die(mysql_error());
     
   header("Location: succesfulpost.php");
   
}

?>

Link to comment
Share on other sites

Ok with the following code, it puts the poster in the entry (content) field and leaves the poster field blank.

 

<?php
$poster = $_COOKIE['user'];
$date =mysql_real_escape_string(date("F j,Y"));
$entry = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (entry, poster, date) VALUES('$entry','$poster','$date')";
   
   $res=mysql_query($sql)or die(mysql_error());

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (entry, poster, date) VALUES('$entry','$poster','$date')";
     
   $res=mysql_query($sql1)or die(mysql_error());
     
   header("Location: succesfulpost.php");
   
}

?>

Link to comment
Share on other sites

try this then

<?php
$poster = $_COOKIE['user'];
$poster=mysql_real_escape_string($_POST['poster']);
$date =mysql_real_escape_string(date("F j,Y"));
$entry = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (entry, poster, date) VALUES('$entry','$poster','$date')";
   
   $res=mysql_query($sql)or die(mysql_error());

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (entry, poster, date) VALUES('$entry','$poster','$date')";
     
   $res=mysql_query($sql1)or die(mysql_error());
     
   header("Location: succesfulpost.php");
   
}

?>

Link to comment
Share on other sites

Omg I've been a total noob. When testing the form I was writing Cody (my name) as the message which is also the same thing that would have been coming from the COOKIE. That is why it was putting it in the wrong field (which it wasn't). Real sorry about that mate.

 

BUT the poster is not showing up from the cookie. I just realised something else too. I was putting the cookie as $_COOKIE['user'] when it should have been the name of the cookie....Sorry.

 

the $_COOKIE['ID_my_site'];

 

Sorry bout that muck ups mate..and really appreciate your help! Let me check this and see if it works.

Link to comment
Share on other sites

Ok. The cookie correction didn't work. IT is still inserting to the DB with no poster

 

<?php

$poster = $_COOKIE['ID_my_site'];
$poster=mysql_real_escape_string($_POST['poster']);
$date =mysql_real_escape_string(date("F j,Y"));
$entry = mysql_real_escape_string($_POST['entry']);

if($_POST['type'] == 'mainsite'){
   
   $sql="INSERT INTO mainsite (entry, poster, date) VALUES('$entry','$poster','$date')";
   
   $res=mysql_query($sql)or die(mysql_error());

   header("Location: succesfulpost.php");
   
}
   
if($_POST['type'] == 'adminsite'){
   
   $sql1="INSERT INTO adminsite (entry, poster, date) VALUES('$entry','$poster','$date')";
     
   $res=mysql_query($sql1)or die(mysql_error());
     
   header("Location: succesfulpost.php");
   
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.