Page 1 of 1

Getting info from DB based on form submission - SOLVED!

Posted: Mon Jul 23, 2012 5:16 pm
by Thunderbob
So I am still working on a pm system and it is 90% finished.

Here is the form being submitted. This is the "send message" form
<form name="form id="form" method="POST">
Send To:<br>
<input type="text" name="to_user" id="to_user" style="width:729px" value="" /><br /> 
Subject:<br>
<input type="text" name="subject" id="subject" style="width:729px" value=""/><br /> 
Message:<br> <textarea name="message" id="message" rows="3" cols="106"></textarea><br /> 
<input type="submit" name="send" id="send" value="Send" /><br>
<input type="hidden" name="to_userid" id="to_userid"  value="<?php print $toid ?>"/>
<input type="hidden" name="userid" id="userid"  value="<?php print $user_id; ?>"/>
<input type="hidden" name="from_user id="from_user"  value="$username" />
<input type="hidden" name="senddate" id="senddate"  value="<?php echo date("l, js F Y, g:i:s a"); ?>"/>
You will not be notified if a message is sent. We are still working on that feature. Messages successfully sent will show in your outbox.
<div> <?php $sent = $_COOKIE['sent']; print $sent ?></div>
<?php

if($_POST['send']){
   $to_username = $_POST['to_user'];
  $subject =     $_POST['subject'];
  $message =     $_POST['message'];
  $to_userid =   $_POST['to_userid'];
  $userid = $_POST['userid'];  
  $from = $_POST['from_user'];
  $senddate = $_POST['senddate'];
  


$query = mysql_query("SELECT `id_user` FROM `fgusers3` WHERE `username` ={$to_username}");
if (mysql_num_rows($query)==0){
	$row = mysql_fetch_assoc($query);
 	$toid = $row['id_user'];}
$query1 = mysql_query("SELECT `imagelocation` FROM `fgusers3` WHERE `username` ={$from}");
if (mysql_num_rows($query1)==0){
	$row = mysql_fetch_assoc($query1);
 	$receiveimage = $row['imagelocation'];
        echo "<img src='$receiveimage'></a>";

 }
  

 $query = mysqli_query($myConnection, "INSERT INTO pm_outbox (userid, username, to_id, to_user, title, content, date) VALUES ('$userid', '$username', '$toid', '$to_username', '$subject', '$message', '$senddate')") or die(mysqli_error($myConnection));

$query = mysqli_query($myConnection, "INSERT INTO pm_inbox (from_id, from_user, title, content, date, userid) 
VALUES ('$user_id','$username','$subject', '$message', '$senddate', '$toid')") or die(mysqli_error($myConnection));

header("Location: http://www.yourtechview.com/source/test ... d=$user_id");

my apologies for using '  ' instead of { }
   
}

?>

</form>
The main thing I am trying to put into the database is a variable called $toid

$toid is the user id of the person I am sending the message to.

I am trying to get the database to get the id of the user who's username matches the "send to" part of the form.
Of course this doesn't happen and I keep getting 0

Any help? I can see the messages in my database and sent box but they don't reach the inbox of the receiver.

Re: Getting information from DB based on form submission

Posted: Mon Jul 23, 2012 5:30 pm
by Temor
You need to encase your variable in quotes if it contains a string.
$query = mysql_query("SELECT `id_user` FROM `fgusers3` WHERE `username` ={$to_username}");
Needs to be:
$query = mysql_query("SELECT `id_user` FROM `fgusers3` WHERE `username` ='{$to_username}' ");
The same applies to $query1
$query1 = mysql_query("SELECT `imagelocation` FROM `fgusers3` WHERE `username` ={$from}");
$query1 = mysql_query("SELECT `imagelocation` FROM `fgusers3` WHERE `username` ='{$from}' ");

Re: Getting information from DB based on form submission

Posted: Mon Jul 23, 2012 5:51 pm
by Thunderbob
No dice.


Is it silly for me to believe the location of the query relative to the form will change something?

Re: Getting information from DB based on form submission

Posted: Mon Jul 23, 2012 10:36 pm
by Temor
Is this line giving you anything?
die(mysqli_error($myConnection));
try changing that to die to echo.

Re: Getting information from DB based on form submission

Posted: Tue Jul 24, 2012 1:00 am
by jacek
The value of mysqli_error() is reset after every query, try adding it under the SELECTs too and see if that dhows anything up :)

Re: Getting information from DB based on form submission

Posted: Tue Jul 24, 2012 1:04 am
by Thunderbob
Temor:

Doesn't seem to do anything.
The messages are still being inserted into my database just without that variable.

Jacek

I just want to make sure I am following your instructions properly....like this?
$query = mysql_query("SELECT `id_user` FROM `fgusers3` WHERE `username` ='{$to_username}' ");
if (mysql_num_rows($query)==0){
	$row = mysql_fetch_assoc($query);
 	$toid = $row['id_user'];}
        echo mysql_error();
$query1 = mysql_query("SELECT `imagelocation` FROM `fgusers3` WHERE `username` ='{$from}'");
if (mysql_num_rows($query1)==0){
	$row = mysql_fetch_array($query1);
 	$receiveimage = $row['imagelocation'];
        echo "<img src='$receiveimage'></a>";
        echo mysql_error();

Re: Getting information from DB based on form submission

Posted: Tue Jul 24, 2012 1:20 am
by Thunderbob
Hey guys I got it working.

I did this
if (mysql_num_rows($query2) > 0){
	$row = mysql_fetch_array($query2);
 	$toid = $row['id_user'];}
        echo mysql_error();

At first it was saying ==0 instead of > 0 which was a mental blunder on my part.

Re: Getting info from DB based on form submission - SOLVED!

Posted: Wed Jul 25, 2012 11:17 pm
by jacek
Thunderbob wrote:At first it was saying ==0 instead of > 0 which was a mental blunder on my part.
:D Glad you spotted since I didn't on the first read through.