topflight Posted October 9, 2008 Share Posted October 9, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/ Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 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()); Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660630 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 P.S that is just the bottom portion of the code. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660631 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 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'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660632 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 Can you echo out the POST requests and $ip to make sure they have values? Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660640 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 You want me to make sure the $ip has a value assign to it? It does have one. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660644 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 I want you to make sure they all have values. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660647 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 oo they do. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660649 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 Hmm not sure. Could you tell me what each field is expecting to receive and what value you're trying to pass into it because it looks like it's receiving something unexpected... Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660654 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 It is receiving information from a form that goes to this page. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660655 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 Does anybody know whats wrong? Would be gladly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660927 Share on other sites More sharing options...
philipolson Posted October 9, 2008 Share Posted October 9, 2008 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']}". Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-660957 Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 I am still receiving a white page. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-661435 Share on other sites More sharing options...
topflight Posted October 10, 2008 Author Share Posted October 10, 2008 Do anybody else have a suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-661464 Share on other sites More sharing options...
AndyB Posted October 10, 2008 Share Posted October 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-661473 Share on other sites More sharing options...
topflight Posted October 10, 2008 Author Share Posted October 10, 2008 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") { Quote Link to comment https://forums.phpfreaks.com/topic/127657-not-inserting-into-database/#findComment-661478 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.