Jump to content

Form not updating database when queried


kittrellbj

Recommended Posts

I have an interesting problem.  Basically, I have a single page with built in switches that include different forms/information based on user input.  (i.e. To read private messages, they click the read link which loads a $_GET value into itself, including the "display all private messages" page.  If they click New Message, it feeds a $_GET value into itself to include the "write a new message" page.)

 

I have verified that the echo statements from:

 

$send_comp = "INSERT INTO archadium_pm (new, id_from, id_to, subject, message) VALUES ('1', '$send_from_result', '$send_to_result', '$send_subject', '$send_msg')";

 

returns the correct values.  It is identitcal to another working piece of code in the registration form that sends the user a new message to their private message inbox.

 

The form validation gets all the way through and then it dies stating that it cannot connect.  The database is connected to correctly during the IF statements, as it requires you to input a valid user ID or username to continue, kicking you back if what you enter does not match a record in the database.

 

Here's the code for the (validation portion of the) form:

 

if (is_numeric($_GET['id']) || (ereg("^[a-zA-Z0-9]{1,16}$",$_POST['id'])))
{
if ($_POST['send_pm'])
{
	$send_to = $_POST['id'];
	if (is_numeric($send_to))
	{
	$send_to_query = "SELECT uid FROM archadium_users WHERE uid='$send_to'";
	}
	elseif (!is_numeric($send_to))
	{
	$send_to_query = "SELECT uid FROM archadium_users WHERE username='$send_to'";
	}
	$send_to_result = mysql_query($send_to_query);
	$send_subject = $_POST['send_subject'];
	$send_msg = $_POST['send_msg'];
	if ($send_to_result < 1)
	{
	$send_toerror = "Please use support to contact Archadium staff.";
	}
	elseif (mysql_num_rows($send_to_result) > 0)
	{
	$session_user = $_SESSION['username'];
	$send_from_query = "SELECT uid FROM archadium_users WHERE username='$session_user'";
	$send_from_result = mysql_query($send_from_query);
	$send_from_result = mysql_fetch_assoc($send_from_result);
	$send_to_result = mysql_fetch_assoc($send_to_result);
	$send_from_result = $send_from_result['uid'];
	$send_to_result = $send_to_result['uid'];
	$send_comp = "INSERT INTO archadium_pm (new, id_from, id_to, subject, message) VALUES ('1', '$send_from_result', '$send_to_result', '$send_subject', '$send_msg')";
	mysql_query($send_comp) or die("Couldn't connect. 715");
	echo "Message sent!";
	exit();
	}
}
elseif (($_GET['id'] < 1) && isset($_GET['id']))
	{
	$send_toerror = "Please use support to contact Archadium staff.";
	}
}
if (!is_numeric($_GET['id']) || (!ereg("^[a-zA-Z0-9]{1,16}$",$_POST['id'])))
{ echo "Could not send message."; }

 

When I echo the database query, it returns:

 

INSERT INTO archadium_pm (new, id_from, id_to, subject, message) VALUES ('1', '1', '1', 'Testing', 'Testing...')

 

The above appears to be a valid MySQL entry. I am kinda stumped on this, not sure why exactly it is dying at the mysql_query to insert a new record.

Link to comment
https://forums.phpfreaks.com/topic/140336-form-not-updating-database-when-queried/
Share on other sites

mysql_query($send_comp) or die("Couldn't connect. 715");

Change that line to

mysql_query($send_comp) or die("Couldn't connect. 715: ".mysql_error());

So you can be 100% sure the query isn't the problem.

 

It's probably the query since it makes it up to that point and dies the way you tell it to.

Ah, thank you.  Yes, that helped a great deal.  I received the error:

 

Duplicate entry '0' for key 1

 

So, I went back and checked and it was a simple mistake in my MySQL table where the message ID field was trying to duplicate the new user PM.  Basically, message ID was neglectfully not set to auto_increment, and thus, was attempting to insert a new record into a record that already existed!  :o

 

Thanks a bunch!

Archived

This topic is now archived and is closed to further replies.

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