Jump to content

[SOLVED] need help


tritus

Recommended Posts

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>';
}



?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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');";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>';

  }

 

 

 

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ();

  }

 

 

?>

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.