PDA

View Full Version : SQL php debug.


Crazydog
07-25-2005, 08:50 PM
So here's the problem:
I have this sql based form. Everything works fine, except it only records the message part of the form.

Here is the code for the form:

PHP Code:
<?php

mysql_connect( 'localhost', 'crazydog', 'password' ) //password removed
or die( "Error! Could not connect to database: " . mysql_error() );

mysql_select_db( 'crazydog' )
or die( "Error! Could not select the database: " . mysql_error() );
$id = $_REQUEST['id'];
if( $id )
{
$query = "SELECT * FROM `requests` WHERE `id`='$id'";

$result = mysql_query( $query );

if( $result && $requests = mysql_fetch_object( $result ) )
{
$Name = $requests -> Name;
$Type = $requests -> Type;
$Message = $requests -> Message;
}
}
?>
<form name="form" method="POST" action="submitform.php">
<input name="Name" type="text" id="Name" size="10" value="<?php echo($Name) ?>">
<input name="Type" type="text" id="Type" size="10" value="<?php echo($Type) ?>">
<textarea name="Message" id="Message"><?php echo($Message) ?></textarea>
<input type="submit" name="Submit" value="Submit">
</form>

And here is the processor:
PHP Code:
<?php
mysql_connect( 'localhost', 'crazydog', 'password' ) //password removed
or die( "Error! Could not connect to database: " . mysql_error() );

mysql_select_db( 'crazydog' )
or die( "Error! Could not select the database: " . mysql_error() );

$id = $_REQUEST['id'];
$Name = $_REQUEST['Name'];
$Type = $_REQUEST['Type'];
$Message = $_REQUEST['Message'];
if( $id )
{
$query = "UPDATE `requests` SET `Name`='$Name', `Type`='$Type', `Message`='$Message' WHERE `id`='$id'";
}
else
{
$query = "INSERT INTO `requests` ( `Name`,`Type`,`Message` )
VALUES ( '$Name','$Type','$Message' )";
}

$results = mysql_query( $query );

if( $results )
{
echo( "Successfully saved the entry.<br />" );
}
else
{
die( "Trouble saving information to the database: " . mysql_error() );
}
?>


If you need any other code, just let me know.

devinemke
07-25-2005, 09:32 PM
on your form processor...
$id = $_REQUEST['id'];
...how would be this ever be set? you are not sending that as part of the form data.

Crazydog
07-25-2005, 10:07 PM
Ok forget that variable. What I'm more worried about are the Name and Type variables that aren't being saved.

sftl99
07-25-2005, 11:11 PM
on your form processor...
$id = $_REQUEST['id'];
...how would be this ever be set? you are not sending that as part of the form data.
The id request is there in the case that he's clicked a link like form.php?id=2, the form would be auto populated with the information from the database then when submitted with the id would update the record instead of creating a new one. Just thought I'd answer that question. The point is, two of the three fields are not working, I can't see why though. Everything seems to line up. Crazydog, you probably ought to check your table to make sure that Name and Type exist.

devinemke
07-25-2005, 11:23 PM
The id request is there in the case that he's clicked a link like form.php?id=2, the form would be auto populated with the information from the database then when submitted with the id would update the record instead of creating a new one. Just thought I'd answer that question.
sorry, but that is not true. the form (as originally posted above) does not ask for the "id" so even if the "id" was passed to the form as a GET var there is nothing to auto populated since there is no form field by that name, and thus it would not then be passed to the form processor. of course if the code as posted is incomplete and this is being handled elsewhere then i that i stand corrected. again, i realize this is not the direct problem at hand but a valid point since the code as written will never update an exiting record.

Crazydog
07-26-2005, 12:00 AM
I don't care about the ID.
I just want to know why Name and Type aren't being recorded but Message is!

Nightfire
07-26-2005, 12:47 AM
IIRC Name and Type are reserved variables names. Try renaming them or something?

Crazydog
07-26-2005, 04:02 AM
will try...

edit
that worked!!!

I have one more question, but I'll post a thread.