Problem with PHP and Mysql

Asked

Viewed 175 times

0

I’m doing a chat (chat) using PHP and Mysql, and I’m having problems.

I think it has to do with the fact that php 5.5 not accept more mysql, (now you need to use mysqli) Well, I have two files:

index php.

<html>
<head>
<title>Chat Box</title>


<script>
function submitChat() {
    if(form1.uname.value == '' || form1.msg.value == '') {
      alert('ALL FIELDS ARE MANDATORY!!!');
      return;
  }
  var uname = form1.uname.value;
  var msg = form1.msg.value;
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState==4&&xmlhttp.status==200) {
      document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg,true);
  xmlhttp.send();
}
</script>


</head>
<body>
<form name="form1">
Enter Your Chatname: <input type="text" name="uname"><br>
Your Message: <br>
<textarea name="msg"></textarea><br>
<a href="#" onclick="submitChat()">Send</a><br><br>
<div id="chatlogs">
LOADING CHATLOGS PLEASE WAIT...
</div>

</body>
</html>

Insert.php

<?
$uname = $_REQUEST['uname'];
$msg = $_REQUEST['msg'];

$con = mysqli_connect('XXX','XXX','XXX','XXX');

mysqli_query($con,"INSERT INTO logs ('username','msg') VALUES ('$uname','$msg')");

$result1 = mysqli_query($con,"SELECT * FROM logs ORDER by id DESC");

while($extract = mysqli_fetch_array($result1)) {
  echo "<span class='uname'>" . $extract['username'] . "</span>: <span class='msg'>" . $extract['msg'] . "</span><br>";
}
?>

I have a domain where the code is: mundozoeira.com.br

So you can go there to see the behavior.

  • What’s the matter? @Renan

  • The chat just doesn’t work. I send the message and nothing happens. I mean, the phrase "LOADING CHATLOGS PLEASE WAIT..." disappears, it is replaced using innerHTML. I have no idea what it is...

  • Could be an error in mysql database?

  • Brother, after your connection put this code. if ( $con ->connect_errno )&#xA; {&#xA; printf("Erro na Conexão: %s\n", $con ->connect_error);&#xA; exit();&#xA; }

  • I already did. Nothing happened.

1 answer

1


The problem is in your query, mysql no use apostrophe (') where we make references to the columns, the correct is:

INSERT INTO logs (`username`,`msg`) VALUES ('$uname','$msg')

It might be a connection error with mysql, try editing the file for something like:

<?php
error_reporting(E_ALL|E_STRICT);

$uname = empty($_REQUEST['uname']) ? NULL : $_REQUEST['uname'];
$msg = empty($_REQUEST['msg']) ? NULL : $_REQUEST['msg'];

if (NULL === $uname || NULL === $msg) {
    echo 'Faltam dados';
    exit;
}

$con = mysqli_connect('XXX','XXX','XXX','XXX');

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}

$resultado = mysqli_query($con,"INSERT INTO logs (`username`, `msg`) VALUES ('$uname','$msg')");

if ($resultado === false) {
    printf("Error: %s\n", mysqli_error($con));
    exit;
}

$result1 = mysqli_query($con,"SELECT * FROM logs ORDER by id DESC");

if ($result1 === false) {
    printf("Error: %s\n", mysqli_error($con));
    exit;
}

while($extract = mysqli_fetch_array($result1)) {
    echo "<span class='uname'>" . $extract['username'] . "</span>: <span class='msg'>" . $extract['msg'] . "</span><br>";
}

mysqli_close($con);
?>

To use the severe accent character in mysql queries, use the key next to the number 1 and above the tab without using the Shift, as in the image:

Teclado Abnt

Other possible problems

  1. Short open tags

    Change <? for <?php

    Note that to use <? you need to be enabled in php.ini, or equal to 1:

    short_open_tag=1
    

    Note also that from PHP5.4 this works without short_open_tag:

    <?='Teste'?>
    

    But this won’t work:

    <?
    echo 'Test';
    ?>
    
  2. Error in file encoding

    If you save the file with different encodings from ANSI and of UTF-8 NO GOOD or other type of encoding, this may cause the PHP execution to fail.

    To fix, always save the file as ANSI or UTF-8 NO GOOD (in case I prefer utf8), for this it is necessary to use an advanced editor like Sublimetext or Notepad++, see:

    Using Notepad++:

    utf8 sem boom notepad++

    Using Sublime Text:

    utf8 sublime sublimetext

    More details on: /a/43205/3635

  3. Apache server without PHP installed, you may be using Apache server without PHP.

    • To install Wamp (Window+Apache+Mysql+php) you can use ready-made packages such as:

    • To install on Ubuntu or debian use apt in the terminal, execute the following commands:

      sudo apt-get update
      sudo apt-get install apache2
      sudo apt-get install php5
      sudo apt-get install mysql-server-5.0
      sudo apt-get install php5-mysql
      sudo /etc/init.d/apache2 restart
      
  • 1

    TOPPP!!!! Your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.