-2

Basically i've been scratching my head at this and I still can't figure out why it's not inserting.

I'm 100% sure the database is connected as it's fetching information just fine, however the following code fails to insert anything into the database. I've checked for spelling mistakes, i've checked from deprecated php code etc, and have used mysqli and mysql.

<?php 

include_once "settings.php";


if (isset($_POST['sendMessage']) && isset($_POST['messageTo']) && isset($_POST['messageBody'])){

    $messageTo = mysql_real_escape_string($_POST['messageTo']);
    $messageBody = mysql_real_escape_string($_POST['messageBody']);

    $query= "INSERT INTO inbox (`msgTo`, `msgFrom`, `msgBody`)
         VALUES('$messageTo', '$username', '$messageBody')";

    if(mysql_query($query))
        echo "done.";
    else
        echo "Problem with Query";
}
?>
            <form method="POST">

        <div class="searchContain">
          <input name="textfield" type="text" name="messageTo" class="input search"><br />

          <textarea placeholder="Your message..." name="messageBody" class="input sendmsg" ></textarea><br />

          <button class="input" name="sendMessage">Send Message</button>
        </div>

        </form> 

Settings.php:

<?php 
session_start();
include_once "../more/config/connect.php";
// Settings //


function logincheck(){
if (!isset($_SESSION['username'])){
    header("location: ../index.php");
}
}

logincheck();

$username=$_SESSION['username'];

$gatherInfo=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");
$fetch=mysql_fetch_object($gatherInfo);

?>

connect.php:

<?php 

// Connect to the server //
date_default_timezone_set('Europe/London');
mysql_connect("localhost", "root", "connected") or die (mysql_error ());

mysql_select_db("ts") or die(mysql_error());
?>

If anyone could help me fix this rather basic rookie error I'd be very grateful!

UPDATE:

Basically after changing the code. I've gone through the MAMP panel and changed the errors so they display. It's giving me the following error message:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)

in I've never come across this error before, any ideas? It seems to fetch data from the database just fine, so I'm not sure why.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jake Duncan
  • 71
  • 1
  • 8
  • 2
    You don't check for errors. That's why you can't figure out what's wrong. Use mysql_error() after your query. – John Conde Aug 08 '14 at 14:21
  • Provide the data you're trying to insert. – ʰᵈˑ Aug 08 '14 at 14:21
  • 1
    FYI, you *are* using depracated code and you *are not* using mysqli. – John Conde Aug 08 '14 at 14:22
  • What version of PHP? mysql_* functions are deprecated in recent versions. See also: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860140#12860140 – Brian Driscoll Aug 08 '14 at 14:24
  • 1
    add some error checking to see if your insert query is fine: `mysql_query($query) or die(mysql_error());` – Latheesan Aug 08 '14 at 14:24
  • are you even joining the if? mysql_real_escape_string is deprecated, of course, but it returns a **string**, not a **boolean**, so you may use isset ($_POST['messageTo']) and **then escape it**... I mean.. you're already escaping them, why do you check in the if if they are escaped..? pointless, and probably wrong. – briosheje Aug 08 '14 at 14:26
  • 1
    @briosheje a non-empty string will resolve to `true` in PHP. – Brian Driscoll Aug 08 '14 at 14:29
  • php version is 5.4.4 I've added the form into the original post also. And for some reason mysqli doesn't work for anything i'm doing.. so I've been using mysql for the time being. – Jake Duncan Aug 08 '14 at 14:29
  • @BrianDriscoll: what if messageTo is ""? – briosheje Aug 08 '14 at 14:29
  • @briosheje it resolves to `false`, as expected – EaterOfCode Aug 08 '14 at 14:30
  • So what's the point of escaping it to perhaps have it false? – briosheje Aug 08 '14 at 14:30
  • I have no idea, but it's the thought that counts I guess – EaterOfCode Aug 08 '14 at 14:31
  • @briosheje It has been a while, but if memory serves `mysql_real_escape_string("")` will return an empty string, which will resolve to `false`. – Brian Driscoll Aug 08 '14 at 14:34
  • `$username` is undefined, therefore breaking your query. @JakeDuncan Plus, your form doesn't have a form element to support it. That and using `&& mysql_real_escape_string($_POST['messageTo'])` in your conditional statement. – Funk Forty Niner Aug 08 '14 at 14:45
  • Basically after changing the code... I've gone through the MAMP panel and changed the errors so they display... It's saying Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) in I've never come across this error before.. any ideas? It seems to fetch data from the database just fine, so I'm not sure why. – Jake Duncan Aug 08 '14 at 14:46
  • To test, place your entire DB connection code in place where you have `include_once "settings.php";`, plus anything that's inside those included files. If it works, then it will mean that it's a path issue. It might even be a sessions issue. – Funk Forty Niner Aug 08 '14 at 14:58
  • Yeah it's giving me an error.. mysql_connect(): Can't connect to local MySQL server through socket... – Jake Duncan Aug 08 '14 at 15:00
  • *Hm,*... try and restart everything. *Or,* changing `localhost` to `127.0.0.1` - Plus, are you trying to run this entirely on your own machine or by accessing an external DB via WWW? I also suggest you Google `mysql_connect(): Can't connect to local MySQL server through socket.` - I found many possible reasons; it could be anything. – Funk Forty Niner Aug 08 '14 at 15:06
  • Yeah i'm currently in the process of doing so, and it's running on my Macbook pro, I'll give 127 a go! thanks for the help guys, I know how stupid I look right now :') – Jake Duncan Aug 08 '14 at 15:09
  • You're welcome and don't be so hard on yourself Jake. *Gawd,* I wish I could remember where I saw a related question a day or so ago, about someone using a MACbook with a similar problem. Include MACbook in your Google search, you may end up finding that question. – Funk Forty Niner Aug 08 '14 at 15:17
  • Thank's! Changing it to 127.0.0.1 didn't work, I'm going to google to try and find out what it is. It's annoying me as I could have completed so much of the project by now but it's seriously bugging me lol Thanks for the help tho!! – Jake Duncan Aug 08 '14 at 15:33

2 Answers2

0

try changing your query to

$query= "INSERT INTO `inbox` (`msgTo`, `msgFrom`, `msgBody`) 
     VALUES('$messageTo', '$username', '$messageBody')";
MD Singh
  • 1,455
  • 1
  • 9
  • 17
  • is back-ticks really necessary? none of his table name or column are reserved sql keywords. – Latheesan Aug 08 '14 at 14:25
  • I have had issues in some versions of PHP and back ticks solved it. Also make sure that display errors is on. – MD Singh Aug 08 '14 at 14:28
  • Basically after changing the code... I've gone through the MAMP panel and changed the errors so they display... It's saying Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) in I've never come across this error before.. any ideas? It seems to fetch data from the database just fine, so I'm not sure why. – Jake Duncan Aug 08 '14 at 14:46
  • @JakeDuncan the comments section of a post is not the place to be asking a new question. Please [search](http://stackoverflow.com/search) for questions similar to your new one, and if you cannot find one relevant to you, [Ask a Question](http://stackoverflow.com/questions/ask). – esqew Aug 08 '14 at 14:51
0

you can try the following

if (isset($_POST['sendMessage']) && isset($_POST['messageTo']) && isset$_POST['messageBody'])){

    $messageTo = mysql_real_escape_string($_POST['messageTo']);
    $messageBody = mysql_real_escape_string($_POST['messageBody']);

    $query= "INSERT INTO inbox ('msgTo', 'msgFrom', 'msgBody') 
         VALUES('$messageTo', '$username', '$messageBody')";

    if(mysql_query($query))
        echo "done.";
    else
        echo "Problem with Query";
}
  1. Column names should be in single inverted commas
  2. You should check for the mysql_query to give success response.
  3. do not call the same function again and again i.e mysql_real_escape_string was called 2 times for the same thing. Alternatively assign that to a variable, although you need not have escaped the values to check in if condition
  • those are normal commas D: – EaterOfCode Aug 08 '14 at 14:34
  • sorry, wrong name `('msgTo', 'msgFrom', 'msgBody')` those, should be ` instead of `'` – EaterOfCode Aug 08 '14 at 14:37
  • Basically after changing the code... I've gone through the MAMP panel and changed the errors so they display... It's saying Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) in I've never come across this error before.. any ideas? It seems to fetch data from the database just fine, so I'm not sure why. – Jake Duncan Aug 08 '14 at 14:39