-2

I am new in php and sql. I was trying to make a chat web,but i had too many errors in php, it is connected to database but im not able to Insert things in my database. I have two errors at the time that are hard to understand for me,

1

 FATAL ERROR:Call to a member function mysqli_num_rows() on a non-object in E:\xamp\htdocs\chatting\post.php on line 20

2

 NOTICE:undifined index: in E:\xamp\htdocs\chatting\post.php on line 13

the php script that is giving these errors is

  <?php
  $connect= new mysqli('localhost', 'root','' ,'user');



  if($connect->connect_error){

  die('connection failed bruh');

 } else 

 echo 'connected';
 $message= $_POST[`message`];
 $name= $_POST[`name`];

 $sql = 'INSERT INTO user (name, message) VALUES (`{$name}`, `{$message}`)';

 $result = $connect-> query($sql);

 if ($result->mysqli_num_rows($sql) > 0){
  while ($row=$result->fetch_assoc()){
     echo  $row[`$message`];
 }

 } else 
   echo  '<br> problem';
 ?>

my sql database's table name is user and it have three colmns

 1.id (which is set to auto increment)
 2.name
 3.message

if anyone can help me get rid of these will be very thankful and if there are more errors in my php code then please tell me

Community
  • 1
  • 1

2 Answers2

-1

There is a space here: $result = $connect->" "query($sql);, remove it and try again.

Make sure to avoid spacing on improper places.

Also, as I said instead of $message= $_POST[message]; do $message= $_POST['message'];. Don't use backticks on assoc arrays key names.

-1

It looks like you're trying to echo what is being posted but your attempting to do so by creating a loop for an INSERT. You should be looping a SELECT. There are two options for you here. The easiest is to just echo what was submitted:

echo $message;

If what you're trying to do is verify that the query was true, then you can do something like:

if ( $result = $connect-> query($sql) )
{
    echo $message;
} else
{
    echo  '<br> problem';
}
Matt
  • 24
  • 3
  • THANKS IT HAS SOLVED 70% problem of my script – Ahmad Raza Rizvi May 24 '17 at 18:21
  • No prob. On a side note, look into sanitizing the inputs or using PDO once you get things figured out. It'll help patch some vulnerabilities in your script. – Matt May 24 '17 at 18:24
  • ok, I have done that but the message is not displaying but its saving in database – Ahmad Raza Rizvi May 24 '17 at 19:13
  • its displaying PROBLEM i dont know why,.my code has become **connect_error){ die('connection failed bruh'); } else echo 'connected'; $message= $_POST['message']; $name= $_POST['name']; $sql = 'SELECT FROM user (name, message) VALUES (`{$name}`, `{$message}`)'; $result = $connect-> query($sql); if ($result= $connect-> query($sql)) { echo "its done"; echo "the message is:$message"; } else { echo '
    problem'; } ?>**
    – Ahmad Raza Rizvi May 24 '17 at 19:20
  • Your select statement is wrong. You just replaced the word INSERT with SELECT which isn't how it works. Read: https://dev.mysql.com/doc/refman/5.7/en/select.html to learn about select queries. Also, $result = $connect-> query($sql); is stated twice. You only need it in the if($result = $connect-> query($sql) ) for it to fire off the query. If you have questions about your query and its formatting you should probably start another question. – Matt May 24 '17 at 20:03
  • i will do that, and thanks – Ahmad Raza Rizvi May 25 '17 at 11:30