0
    <?php
include "../au.php";

$towhom = $_POST['towhom'];
$content = $_POST['content'];

date_default_timezone_set('Asia/KolKata');
$xyz = date(DATE_RFC2822);

$conn = mysql_connect('localhost', 'local', 'local');
mysql_select_db('chat', $conn);

$q = "INSERT INTO $na (one, two, five, content, ip)
VALUES ('$na', 'sent', '$xyz', '$content', '$ip');";
$w = "INSERT INTO $towhom (one, two, five, content, ip) VALUES ( '$na', 'recieved', '$xyz', '$content', '$ip' );";

if (mysql_query($q)){
echo "<br>&nbsp; Message sent to '".$towhom."'";
} else {
    echo "<br>&nbsp; Failed 1";
}
if (mysql_query($w)){
echo "<br>&nbsp; Message sent to '".$towhom."'";
} else {
    echo "<br>&nbsp; Failed 2";
}
?>

DESCRIPTION : This is messaging script. $towhom and $content are taken from form. $xyz is defined. $na and $ip are defined in au.php file.

PROBLEM : $q is not successful while $w works.

  • Could you please add change to `mysql_query($q) or die(mysql_error())` so you could post the error you are getting ? – Orel Eraki Dec 29 '14 at 18:09
  • `mysql_connect()` extension is deprecated better use `mysqli or PDO` –  Dec 29 '14 at 18:09
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 29 '14 at 18:10

3 Answers3

0

Without any other information, my guess is that the string contained in $towhom does not refer to a valid table. If I'm sending a message to "some_user", the query stored in $w would look something like this:

INSERT INTO some_user (...) VALUES (...)

Do you mean to have a unique table for every user? And if so, are you sure that such tables exist? Some error output would be very useful.

Sculper
  • 756
  • 2
  • 12
  • 24
-1

First of all, Remove unwanted semicolons from your queries

$q = "INSERT INTO $na (one, two, five, content, ip)
VALUES ('$na', 'sent', '$xyz', '$content', '$ip')";
$w = "INSERT INTO $towhom (one, two, five, content, ip) VALUES ( '$na', 'recieved', '$xyz', '$content', '$ip' )";

Then

PROBLEM : $q is not successful while $w works.

If a query is not successful means there could be lot of reasons. You should check it using mysql_query($q) or die(mysql_error())

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27
-1

Think multiline strings are causing issues... Not sure though...

Try,

$q = "INSERT INTO $na (one, two, five, content, ip) " +
     "VALUES ('$na', 'sent', '$xyz', '$content', '$ip')";
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126