simple PHP + MySQL problem... but can't figure it out!

liunx

Guest
Hi there,

More help required I'm afraid. Just getting my teeth into PHP and having all sorts of fun! (yeah...)

I am wanting to just update a table in MySQL and am using an HTML form to submit the various values to the PHP process:

---------------------------------------------------------------------------------------

<form action="insert_media.php" method="post">
<table border="0" width="50%" cellspacing="1" cellpadding="1">

<tr>
<td>Headline:</td>
<td><input type="text" name="headline" maxlength=100></td>
</tr>

<tr>
<td>Headline link:</td>
<td><input type="text" name="headline_link" maxlength=100></td>
</tr>

<tr>
<td>Date:</td>
<td><input type="text" name="date" maxlength=100></td>
</tr>

<tr>
<td>Source:</td>
<td><input type="text" name="source" maxlength=100></td>
</tr>

<tr>
<td>Source Link:</td>
<td><input type="text" name="source_link" maxlength=100></td>
</tr>

<tr>
<td><input type="reset" name="Reset" value="Reset" /></td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>

</table>
</form>

---------------------------------------------------------------------------------------

In "insert_media.php" (the target) I have the following PHP:

---------------------------------------------------------------------------------------

<html>
<head>
<title>Entry Results</title>
</head>
<body>

<h1>Entry Results</h1>

<?php


if ( !$headline || !$headline_link || !$date || !$source || !$source_link )
{
echo "You have not entered all the required details.<br />Please go back and try again." ;
exit ;
}

$headline = addslashes( $headline ) ;
$headline_link = addslashes( $headline_link ) ;
$date = addslashes( $date ) ;
$source = addslashes( $source ) ;
$source_link = addslashes( $source_link ) ;

@ $db = mysql_pconnect( "$host", "$php", "$pwd" ) ;

if (!$db)
{
echo "Error: could not connect" ;
exit ;
}

mysql_select_db( "news" ) ;

$query = "INSERT INTO media VALUES ( '".$headline."' , '".$headline_link."' , '".$date."' , '".$source."' , '".$source_link."' )" ;

$result = mysql_query( $query ) ;
if ($result) echo mysql_affected_rows()." news items inserted into database." ;

?>

</body>
</html>

---------------------------------------------------------------------------------------

Should be simple right? However, the script always falls over at the first "if" statement:

if ( !$headline || !$headline_link || !$date || !$source || !$source_link )

Is this the correct syntax? I copied it from a book, so assumed it was correct. I tried to add a semicolon at the end of the "if" statement, but this didn't make any difference.

I just keep getting the first echo statement coming out on my browser:

"You have not entered all the required details.
Please go back and try again. "

I guess at least this is working :)

I am convinced this is simple and it is just my sleepy eyes that are missing something. Any ideas anyone?

Thanks in advance.

- Tatlarif ( !$headline || !$headline_link || !$date || !$source || !$source_link )
{
echo "You have not entered all the required details.<br />Please go back and try again." ;
exit ;
}

that usually looks like this

if ( !$headline || !$headline_link || !$date || !$source || !$source_link ) {
echo "You have not entered all the required details.<br />Please go back and try again." ;
exit ;
}

but normally if you are submitting it in a form you need to use the super globals especially if your register_globals is OFF. this super global is $_POST, there is more but that is the one you need. so wha tyou have should look like this

if ( !$_POST['headline'] || !$_POST['headline_link'] || !$_POST['date'] || !$_POST['source'] || !$_POST['source_link'] ) {
echo "You have not entered all the required details.<br />Please go back and try again." ;
exit ;
}

that should workThanks Scoutt.

2 points:

1. I had orginally tried to convert the variables by using the code below:

$headline = $_REQUEST['headline'] ;
$headline_link = $_REQUEST['headline_link'] ;
$date = $_REQUEST['date'] ;
$source = $_REQUEST['source'] ;
$source_link = $_REQUEST['source_link'] ;

But I guess I should have been using $_POST ?
Would this still have worked?

2. I inserted your code and get the following error:

"Parse error : parse error, unexpected '!' in insert_media.php on line 11"

which is the line you gave me:

if_(_!$_POST['headline']_||_!$_POST['headline_link']_||_!$_POST['date']_||_!$_POST['source']_||_!$_POST['source_link']_)_{

Any ideas? I can't see what would throw this error - maybe I have setup/not setup some component in my PHP installation?

Thanks,

- Tatlaroops!

the online 'cut and paste' seems to have added a load of underscores in the line. It actually reads:

if_(_!$_POST['headline']_||_!$_POST['headline_link']_||_!$_POST['date']_||_!$_POST['source']_||_!$_POST['source_link']_)_{that is not the line I gave you.

correct, take out all the underscoresshould be this

if(!$_POST['headline'] || !$_POST['headline_link'] || !$_POST['date'] || !$_POST['source'] || !$_P
OST['source_link'] ) {doh! its done it again :(

suffice to say there are NOT a load of underscores in the line causing the error...if there is no _ in there then the line is correct. it will not error out on the ! if front of the varaible so it must be something else.

what is the line after that?I just tried this, but still get the same error:

if ( !${_POST['headline']} || !${_POST['headline_link']} || !${_POST['date']} || !${_POST['source']} || !${_POST['source_link']} ) {

The next part of the script with a "!" is here:

@ $db = mysql_pconnect( "$localhost", "$php", "$pwd" ) ;

if (!$db)
{
echo "Error: could not connect" ;
exit ;
}Originally posted by tatlar
I just tried this, but still get the same error:

if ( !${_POST['headline']} || !${_POST['headline_link']} || !${_POST['date']} || !${_POST['source']} || !${_POST['source_link']} ) {

The next part of the script with a "!" is here:

@ $db = mysql_pconnect( "$localhost", "$php", "$pwd" ) ;

if (!$db)
{
echo "Error: could not connect" ;
exit ;
}
you can't add {} to the variable like that.

if(!$_POST['headline'] || !$_POST['headline_link'] || !$_POST['date'] || !$_POST['source'] || !$_POST'source_link'] ) {

all one line. there is nothing wrong with that line now.I just turned 'show invisibles' on in BBEdit and found a few strange characters in that line. I removed them.

The line:

if ( !$_POST['headline'] || !$_POST['headline_link'] || !$_POST['date'] || !$_POST['source'] || !$_POST['source_link'] ) {

now works, but the script dies after that. I am guessing I need to check that my mysql commands work with a different script

Thanks for the help Scoutt.

- Tatlarok now I just seen this

@ $db = mysql_pconnect( "$host", "$php", "$pwd" ) ;


which is not valid. try this

$db = mysql_pconnect( "$host", "$php", "$pwd" ) ;

and make sure you have the correct password and username.

also onthis line
$query = "INSERT INTO media VALUES ( '".$headline."' , '".$headline_link."' , '".$date."' , '".$source."' , '".$source_link."' )" ;

you need to make all those variables the same as we did in that if statement. you need to add _POST[""] to them, so liek this

$query = "INSERT INTO media VALUES ( '".$_POST["headline"]."' ,Thanks Scoutt,

I will try this out.

- Tatlar
 
Back
Top