Jump to content

Recommended Posts

I am trying to insert some code in the database and all I am receiving is just a white screen please help. I have the Database connected successfully.

 

this is the code

 

<? if ($error){ echo $error_msg; } else {

$insert = mysql_query("INSERT INTO news(authorname,date,newstitle,news,ip) 
VALUES ('$_POST[authorname]','$now','$_POST[newstitle]','$_POST[news]','$ip')");

echo 'News Submitted In Database';

}

}

?>

 

thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/
Share on other sites

You should have single quotes in your POST methods, but add this mysql_error function at the end to make sure it's not your query.  And don't assign it to a variable.

 

mysql_query("INSERT INTO news
(authorname, date, newstitle, news, ip)
VALUES ('$_POST['authorname']','$now','$_POST['newstitle']','$_POST['news']','$ip')") or die(mysql_error());

I am now receiving this message

 

se error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\addnewsv.php on line 50

 

for

 

<?php
if ($error){ echo $error_msg; } else {


mysql_query("INSERT INTO news(authorname, date, newstitle, news, ip)
VALUES ('$_POST['authorname']','$now','$_POST['newstitle']','$_POST['news']','$ip')") or die(mysql_error());

echo 'News Submitted In Database';

}

}

?>

Several parts is wrong, with the most obvious being the parse error that "$foo['bar']" creates. Here's a rewrite:

<?php
if ($error) { 

echo $error_msg; 

} else {

// Validate this database before it gets here, but for now:
$authorname = empty($_POST['authorname']) ? '' : mysql_real_escape_string($_POST['authorname']);
$newstitle  = empty($_POST['newstitle'])  ? '' : mysql_real_escape_string($_POST['newstitle']);
$news       = empty($_POST['news'])       ? '' : mysql_real_escape_string($_POST['news']);
$now        = empty($now)                 ? '' : mysql_real_escape_string($now);
$ip         = empty($ip)                  ? '' : mysql_real_escape_string($ip);

$sql = "INSERT INTO 
			news(authorname, date, newstitle, news, ip) 
		VALUES 
			('$authorname','$now','$newstitle','$news','$ip')";

$res = mysql_query($sql);

if (!$res) {
	echo "Error, I will handle this better but for now will print out some stuff.";
	echo "Like, the SQL statement is ($sql)";
	echo "And the MySQL error is: " . mysql_error();
	exit;
}

echo 'News Submitted In Database';

}
?>

Also note that a simple fix would be to replace "$foo['bar']" with either "$foo[bar]" or "{$foo['bar']}".

Do anybody else have a suggestion?

 

Sure. When you get a white page, view the html source and post exactly what you see.

 

If that doesn't do it, then turn on php error trapping and error display. You can google both those for methods, or check the php online manual.

 

Hint:

 

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

It's hard for us to help you through an error (or more) when you tell us you have an error in line X and leave us to guess which is line X and/or when all we see is 4 or 5 lines of a larger script.

This is the error code

 

Notice: Use of undefined constant newstitle - assumed 'newstitle' in C:\xampp\htdocs\addnewsv.php on line 21

 

And yes I have a variable define in their expect it is called newstitle I changed it. Thwe news title is the name under a form in the form page. Forgive me as I am new to php. Here is line 21

 

<?php
if ($_POST[newstitle]=="1")      
{

 

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.