tritus Posted September 5, 2007 Share Posted September 5, 2007 maybe someone can take a look at this... im trying to get it to getpost and store it in database please help!!! <?php $dbcnx = @mysql_connect("localhost", "user", "pass****"); if (!$dbcnx) { exit('<p>Unable to connect to the' . 'databse server at this time.</p>'); } else { } if (!@mysql_select_dv('tritusco_news')) { exit('<p>Unable to locate the news '. 'database at this time</p>'); } $subject=$_POST['subject']; $body=$_POST['body']; $sql = 'INSERT INTO `tritusco_news`.`news` (`subject`, `body`) VALUES ($subject, $body);'; if (@mysql_query($sql)) { echo '<p>News added</p>'; } else { echo '<p>Error adding News</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/ Share on other sites More sharing options...
vijayfreaks Posted September 5, 2007 Share Posted September 5, 2007 Hi.. see the following code: $dbcnx = @mysql_connect("localhost", "user", "pass****"); if(!$dbcnx) { $db_sel = mysql_select_db('db_name',$dbcnx); if($db_sel) { $subject=$_POST['subject']; $body=$_POST['body']; // before adding it in to db do some validation over subject and body posted var.. // is it contain valid data... $sql = 'INSERT INTO `tritusco_news`.`news` (`subject`, `body`) VALUES ($subject, $body);'; if (@mysql_query($sql)) { echo '<p>News added</p>'; } else { echo '<p>Error adding News</p>'; } } } Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342029 Share on other sites More sharing options...
recklessgeneral Posted September 5, 2007 Share Posted September 5, 2007 Hi Tritus, You don't actually say what the errors are that you're seeing, but I've noticed a couple of problems: 1. Call to mysql_select_dv should be mysql_select_db. 2. You are using single quotes instead of double quotes in the line $sql= ... This has the effect of not expanding the variables so will insert the literal $subject into the database instead of the contents of the variable. Change to $sql = "INSERT INTO `tritusco_news`.`news` (`subject`, `body`) VALUES ($subject, $body)"; 3. I also removed the semi-colon from the insert statement. Might not be a problem but I don't use them. Cheers, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342030 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 sry, im new with php and what im trying to do is set up a spot on my site im working on to add news and store it in the database.... can someone tell me whats wrong with the code i wrote? i tried doing what both said above and still isn't storing it into database... Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342184 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 can someone tell me whats wrong with the code i wrote? Being new to php isn't really an excuse for being new to asking a question. You need to explain what you expect your script to do and what it actually does. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342196 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 ps: The first thing you should do is remove all occurances of the @ error supressor. Errors are good while developing. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342198 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 like i said im trying to get my script to pull the info from the form and store it in the database, then i plan on having it so it pulls the info from the database and displays it on my main page heres where im testing the script.... http://tritusco.com/news/index.php Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342210 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 And what results are you getting? Did you remove the error supressor? Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342213 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 Firstly, your query is foo bar'd. You can't insert into two tables at once and string values need to be surrounded by quotes. Your query is also within single quotes which meens your variables wont work. Try... $sql = "INSERT INTO news (subject, body) VALUES ('$subject', '$body');"; Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342216 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 hey i left out a detail my database has date subject body... i thought that i wouldnt have to do anything with php to add the date thought it would update automatically... sorry if im asking stupid questions ... but i do appreciate ur help .... what im asking is how do i make it so that when its submitted to the database it updates the date.. maybe even have it so its date and time? thanks Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342227 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 hey i left out a detail my database has date subject body... i thought that i wouldnt have to do anything with php to add the date thought it would update automatically... sorry if im asking stupid questions ... but i do appreciate ur help .... what im asking is how do i make it so that when its submitted to the database it updates the date.. maybe even have it so its date and time? thanks so i been working on my database this are the fields i got id date subject body when it stores it in my database though the id, subject, and body work... but the date comes out as 0000-00-00 00:00:00 and then after it stores one thing in the database it wont store any thing else and my error shows up Error adding News.... hope this is enough info for someone to help thx Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342271 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342284 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 <?php $dbcnx = @mysql_connect("localhost", "namw", "******"); if (!$dbcnx) { exit('<p>Unable to connect to the' . 'databse server at this time.</p>'); } else { } if (!@mysql_select_db('tritusco_news')) { exit('<p>Unable to locate the news '. 'database at this time</p>'); } $subject=$_POST['subject']; $body=$_POST['body']; $sql = "INSERT INTO news (subject, body) VALUES ('$subject', '$body');"; if (@mysql_query($sql)) { echo '<p>News added</p>'; } else { echo '<p>Error adding News</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342291 Share on other sites More sharing options...
recklessgeneral Posted September 5, 2007 Share Posted September 5, 2007 Could you post the errors you're getting from the database: if (mysql_query($sql)) { echo '<p>News added</p>'; } else { echo mysql_error (); } Also, what constrainst have you got on your tables? Does the date have to be unique (which would explain why it fails a second time), is your ID field set up correctly (for example, is it set to primary key and auto increment set etc). These point to problems with inserting a second row. To get the date to be something more interesting, update your INSERT statement to something like: "INSERT INTO news (subject, body, date) VALUES ('$subject', '$body', NOW())" If you have phpMyAdmin installed it is often worth trying out your queries with that first - saves poking around with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342293 Share on other sites More sharing options...
trq Posted September 5, 2007 Share Posted September 5, 2007 You still have not removed the @ error supressor! Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342296 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 Duplicate entry '0' for key 1 and i set id to primary and date to unique? <?php $dbcnx = mysql_connect("localhost", "tritusco_jeff", "11jeff20g1986"); if (!$dbcnx) { exit('<p>Unable to connect to the' . 'databse server at this time.</p>'); } else { } if (!mysql_select_db('tritusco_news')) { exit('<p>Unable to locate the news '. 'database at this time</p>'); } $subject=$_POST['subject']; $body=$_POST['body']; $sql = "INSERT INTO news (subject, body) VALUES ('$subject', '$body');"; if (mysql_query($sql)) { echo '<p>News added</p>'; } else { echo mysql_error (); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342302 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 I think it's because You're not inserting a date, so it inserted 0 on the first one. Now it can't because 0 is already taken, it's not unique. Insert the date also. Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342305 Share on other sites More sharing options...
tritus Posted September 5, 2007 Author Share Posted September 5, 2007 sweet deal it works now!!!! thanks guys!!!! Quote Link to comment https://forums.phpfreaks.com/topic/68045-solved-need-help/#findComment-342306 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.