Problem with PHP and MySQL

0

I'm doing a chat using PHP and MySQL, and I'm having problems.

I think it has to do with the fact that php 5.5 does not accept mysql anymore, (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: worldhostel.com

Then you can go there to see the behavior.

    
asked by anonymous 07.07.2015 / 22:45

1 answer

1

The problem is in your query , in mysql we do not use apostrofo ( ' ) in where we reference the columns, the correct one is:

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

It may 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 underscore character in queries mysql , use the key next to the number 1 and above the tab without using Shift , as in the image:

Otherpossibleproblems

  • Shortopentags

    Change%with%by%with%

    Notethattouse<?youneedtobeenabledinphp.ini,orequalto<?php:

    short_open_tag=1

    NotealsothatasofPHP5.4thisworkswithout<?:

    <?='Teste'?>

    Butthiswillnotwork:

    <?echo'Test';?>
  • Errorinfileencoding

    IfyousavethefilewithencodingsotherthanANSIandUTF-8withoutBOMorotherencoding,thiscancausePHPtofailtorun.

    Tofix,alwayssavethefileasANSIorUTF-8withoutBOM(inthiscaseIpreferutf8),soyouneedtouseanadvancededitorlikeSublimeTextorNotepad++,see:

    Usingnotepad++:

    Using Sublime Text:

    Moredetailsat: link

  • Apache Server without PHP installed, you may be using the Apache server without PHP.

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

    • To install on Ubuntu or debian use 1 on the terminal, run 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
      
  • 08.07.2015 / 00:44