Jump to content


Photo

Data problems adding to DB


  • Please log in to reply
3 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 19 June 2006 - 08:22 AM

Hi Guys,

This is driving me mental! I have a simple form:

<?php

session_start();

//Connect to DB 
include("*************");

// POST variables 
$section=$_POST['section'];
$added_by=$_POST['added_by'];
$headline=$_POST['headline'];
$opening=$_POST['opening'];
$body_text=$_POST['body_text'];


if (empty ($_POST['opening']) || empty ($_POST['body_text']))
{
  $msg = "There is an error";
}
if (!isset ($error))
{
 $query = "INSERT INTO cms_stories(section,added_by,headline,opening,body_text) VALUES ('$section','$added_by','$headline','$opening','$body_text')";
  $msg = "A new Story has been added to the database - Please Click <a href=\"http://index\">Here</a> to return to the main menu";
  
  $link = mysql_connect; 
  mysql_select_db($db); 
  $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
  mysql_close(); 
}
?>

<html>
<head>
<title>Add to DB</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<form action="add_blank.php" method="post" name="frmAdd">
  <p><?php echo $msg?></p>
  <table width="737" border="0" cellspacing="1" cellpadding="1">
  <tr> 
    <td>Section</td>
    <td> 
      <select name="section">
        <option value="news">News</option>
        <option value="sport">Sport</option>
        <option value="business">Business</option>
        <option value="viator">Viator</option>
      </select>
    </td>
  </tr>
  <tr> 
    <td>Added By</td>
    <td> 
      <input name="added_by" type="text" size="35" maxlength="128" value="">
    </td>
  </tr>
  <tr> 
    <td height="35">Headline</td>
    <td height="35"> 
      <input type="text" size="48" maxlength="255" name="headline">
    </td>
  </tr>
  <tr> 
    <td>Opening Paragraph</td>
    <td> 
      <textarea name="opening" wrap="virtual" rows="4" cols="80"></textarea>
    </td>
  </tr>
  <tr> 
    <td>Body Text:</td>
    <td> 
      <textarea name="body_text" wrap="virtual" rows="25" cols="80"></textarea>
    </td>
  </tr>
</table>
<input type="Submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Story">
</form>
</body>
</html>

Now I want the form to submit the data to the database:
UNLESS
The body_text and opening textboxes contain no values
IF this is the case
Echo a message to the screen and don’t submit the data
IF they do contain data
Then the data will be submitted

But the form loads with the message ‘A new Story has been added to the database - Please Click here

As shown [a href=\"http://www.slougheaz.org/greenock/new_news/add_tester.php\" target=\"_blank\"]http://www.slougheaz.org/greenock/new_news/add_tester.php[/a]

And it ignores my rules!

Can anyone please advise as this is driving me potty!

Thanks

Chris

#2 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 19 June 2006 - 08:48 AM

$msg = "There is an error";
}
if (!isset ($error

I don't see where $error is set...

Also, why not just do this instead of setting the message variable?
if(form data was empty){
echo "There is an error";
exit();
} else {
process here;
}

If you wish to show the form again, you could stick the form as being part of a function, and call the function when the page is loaded. If post data is no of value, and it was submitted, instead of exit instantly - it could just call the function to display the form once more...

#3 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 19 June 2006 - 10:10 AM

Aggghh!!!

Thanks:

I've modified my form now to say:

1) IF Submit is hit but 'opening' and 'body' are empty then output an error
2) IF there is no error then submit the data

I'm pretty sure i'm nearly there, but it does not like this line:

empty ($_POST['opening']) || empty ($_POST['body_text'])

Parse error: parse error, unexpected T_VARIABLE in *********/*****/new_news/*****.php on line 19

<?php

session_start();

//Connect to DB 
include("*****");

// POST variables 
$section=$_POST['section'];
$added_by=$_POST['added_by'];
$headline=$_POST['headline'];
$opening=$_POST['opening'];
$body_text=$_POST['body_text'];


if (isset($_POST['Submit']))
{
  empty ($_POST['opening']) || empty ($_POST['body_text']) 
  $error = "There is an error";
}
if (!isset ($error))
{
 $query = "INSERT INTO cms_stories(section,added_by,headline,opening,body_text) VALUES ('$section','$added_by','$headline','$opening','$body_text')";
  $msg = "A new Story has been added to the database - Please Click <a href=\"http://index\">Here</a> to return to the main menu";
  
  $link = mysql_connect; 
  mysql_select_db($db); 
  $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
  mysql_close(); 
}
?>

<html>
<head>
<title>Add to DB</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<form action="add_blank.php" method="post" name="frmAdd">
  <p><?php echo $msg?> <?php echo $error?></p>
  <table width="737" border="0" cellspacing="1" cellpadding="1">
  <tr> 
    <td>Section</td>
    <td> 
      <select name="section">
        <option value="news">News</option>
        <option value="sport">Sport</option>
        <option value="business">Business</option>
        <option value="viator">Viator</option>
      </select>
    </td>
  </tr>
  <tr> 
    <td>Added By</td>
    <td> 
      <input name="added_by" type="text" size="35" maxlength="128" value="">
    </td>
  </tr>
  <tr> 
    <td height="35">Headline</td>
    <td height="35"> 
      <input type="text" size="48" maxlength="255" name="headline">
    </td>
  </tr>
  <tr> 
    <td>Opening Paragraph</td>
    <td> 
      <textarea name="opening" wrap="virtual" rows="4" cols="80"></textarea>
    </td>
  </tr>
  <tr> 
    <td>Body Text:</td>
    <td> 
      <textarea name="body_text" wrap="virtual" rows="25" cols="80"></textarea>
    </td>
  </tr>
</table>
<input type="Submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Story">
</form>
</body>
</html>


#4 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 19 June 2006 - 10:45 AM

Probabily because its no longer an if statement, and therefore it's incorrectly stated as well ;)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users