2 Replies - 1904 Views - Last Post: 05 October 2011 - 04:57 AM Rate Topic: -----

#1 wonderlhily  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 19-August 11

Undefined index, mysql_fetch_array expect parameter

Posted 03 October 2011 - 11:45 PM

Error: Notice: Undefined index: ghg in C:\xampp\htdocs\mySalon\shout_index.php on line 18

Notice: Undefined index: ghgh in C:\xampp\htdocs\mySalon\shout_index.php on line 18

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mySalon\shout_index.php on line 22

<?php require_once "connect_database.php"?>

<?php
session_start();

if (isset ($_POST['name'],$_POST['message'],$_POST['submit'])){
    $name=$_POST['name'];

    $message=$_POST['message'];

    $time=date("h:ia d/j/y");

$submit = $_POST['submit'];

if ($submit) {

$query1="INSERT INTO shouts (user_id,username,message,time) VALUES ('$_POST[$name]', '$_POST[$message]')";

    $result = mysql_query("SELECT * FROM shouts ORDER by id DESC limit 5");

    while($r=mysql_fetch_array($result))

    {

        $time=$r["time"];

        $id=$r["id"];

        $message=$r["message"];

        $name=$r["name"];

        echo $name."<br/>".$message."<br/>".$time."<br/>";
}
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>My ShoutBox</title>

    </head>

    <body>

        <form action="shout_index.php" method="post" >

            Name:&nbsp;&nbsp;&nbsp;&nbsp;

                <input type="text" name="name" size=30 maxlength='100'></input><br/>

            Message:

                <input type="text" name="message" size=30 maxlength='100'></input><br/>

                <input type="submit" name="submit" value="submit"></input>

        </form>

    </body>

</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Undefined index, mysql_fetch_array expect parameter

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Undefined index, mysql_fetch_array expect parameter

Posted 04 October 2011 - 01:05 AM

obviously, you don’t have the values $_POST['ghg'] and $_POST['ghgh'] which I think is caused by line 18: $query1="INSERT INTO shouts (user_id,username,message,time) VALUES ('$_POST[$name]', '$_POST[$message]')"; where $name and $message are the values taken from the POST array (lines #7 & #9) and used as key for the same (which hardly would match).

following that, the SQL query fails and due to the lack of proper error handling emits said warning.

additionally, your code is susceptible to SQL Injection attacks. which would be avaoided by either using the immune Prepared Statements or by using sanitisation functions (mysql_real_escape_string()).

further you should use a HTML doctype (not an XHTML one) since you serve it as HTML anyways (your code wouldn’t render as XHTML). hence using <input> & <br> without the (self)closing tags.

note for others: <input></input> is correct (only) in XML-XHTML (when served as such). for the same reason <div/> only works as intended in said XHTML.

This post has been edited by Dormilich: 04 October 2011 - 01:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Undefined index, mysql_fetch_array expect parameter

Posted 05 October 2011 - 04:57 AM

I agree to the above, and you should also start your mysql connection because it's returning FALSE on the query. As for the other errors, I agree with Dormilich that that's it. Although you never use that line...is that supposed to be like that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1