Capitan Jack Posted May 17, 2008 Share Posted May 17, 2008 Hi all, I am having some trouble with INSERT. Everytime I visit the page I get blank screen. I checked my database and I know it is not updating. What am I doing wrong? <?php ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if ($dbc = @mysql_connect ('mysql.smith.robots.net', 'smith', '123456')) { if (!@mysql_select_db ('smith')) { $query = "INSERT INTO blog_posts (id, title, posted_at, excerpt, body, categories) VALUES (0, '{$_POST['title']}', '{$_POST['posted_at']}', '{$_POST['excerpt']}', '{$_POST['body']}', '{$_POST['categories']}'"; if (@mysql_query ($query)) { print "<p>The blog entry has been added.</p>"; } else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>"; } mysql_close(); }} ?> Link to comment https://forums.phpfreaks.com/topic/106019-blank-screen-with-mysql/ Share on other sites More sharing options...
Trium918 Posted May 17, 2008 Share Posted May 17, 2008 normally there is a syntax error with in your code if there is a blank page being displayed. you are missing a ) at the end of your query. <?php VALUES (0, '{$_POST['title']}', '{$_POST['posted_at']}', '{$_POST['excerpt']}', '{$_POST['body']}', '{$_POST['categories']}'"; ?> shoud be <?php VALUES (0, '{$_POST['title']}', '{$_POST['posted_at']}', '{$_POST['excerpt']}', '{$_POST['body']}', '{$_POST['categories']}'"); ?> Link to comment https://forums.phpfreaks.com/topic/106019-blank-screen-with-mysql/#findComment-543358 Share on other sites More sharing options...
ratcateme Posted May 17, 2008 Share Posted May 17, 2008 also u are suppressing errors if it fails to connect and not display a message if it fails i would suggest adding a else statement to your first 2 if statements. because you will get a blank page if the database connection fails. Scott. Link to comment https://forums.phpfreaks.com/topic/106019-blank-screen-with-mysql/#findComment-543371 Share on other sites More sharing options...
phpzone Posted May 17, 2008 Share Posted May 17, 2008 if (!@mysql_select_db ('smith')) { $query = "INSERT INTO blog_posts (id, title, posted_at, excerpt, body, categories) VALUES (0, '{$_POST['title']}', '{$_POST['posted_at']}', '{$_POST['excerpt']}', '{$_POST['body']}', '{$_POST['categories']}'"; You're checking if selecting the DB failed and then doing the query if it failed, surely you want to check for success? if ( mysql_select_db('smith') ) Link to comment https://forums.phpfreaks.com/topic/106019-blank-screen-with-mysql/#findComment-543427 Share on other sites More sharing options...
Capitan Jack Posted May 19, 2008 Author Share Posted May 19, 2008 Okay, so I got INSERT to work but now I want to reroute the user to use the header function to send the user to another page. Basically, the id is auto_increment so I have to use whatever my database assigns the next id for the post. I am having trouble being able to retrieve the auto increment id. Here is my code... <?php ob_start(); ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if ($dbc = @mysql_connect (www.123.com, 'smith', '123456')) { } else { die ('<p>Could not connect to mySQL because: <b>' . mysql_error() . '</b></p>'); } if (!@mysql_select_db ('smith')) { } $query = "INSERT INTO blog_posts (id, title, posted_at, excerpt, body, categories) VALUES (0, '{$_POST['title']}', '{$_POST['posted_at']}', '{$_POST['excerpt']}', '{$_POST['body']}', '{$_POST['categories']}')"; if (@mysql_query ($query)) { echo header ('Location: www.123.com/index.php?id={$_GET['id']}'); exit(); } else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>"; } mysql_close(); ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/106019-blank-screen-with-mysql/#findComment-544754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.