Getting info from DB based on form submission - SOLVED!

Ask about a PHP problem here.
Post Reply
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Getting info from DB based on form submission - SOLVED!

Post 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.
Last edited by Thunderbob on Tue Jul 24, 2012 1:21 am, edited 1 time in total.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Getting information from DB based on form submission

Post 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}' ");
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: Getting information from DB based on form submission

Post by Thunderbob »

No dice.


Is it silly for me to believe the location of the query relative to the form will change something?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Getting information from DB based on form submission

Post by Temor »

Is this line giving you anything?
die(mysqli_error($myConnection));
try changing that to die to echo.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Getting information from DB based on form submission

Post 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 :)
Image
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: Getting information from DB based on form submission

Post 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();
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: Getting information from DB based on form submission

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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.
Image
Post Reply