Viewed   172 times

I'm getting an undefined index error with this code:

// Select all bookings
$sql = "SELECT * FROM booking";

$result = mysqli_query($con,$sql);

echo "<p>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // echo $row['custName'] . " - " . $row['roomNb'] . " - " . $row['date'];
    echo $row['custName'];
}

Error:

Notice: Undefined index: custName in C:xampphtdocsalxbookindex.php on line 40

I'm selecting all columns from my table. I have no problem with $row['roomNb'] and $row['date'] but for some reason $row['custName'] is giving me problems. The spelling of custName is correct.

What could be causing this?

 Answers

2

Put a piece of debug code into your script to proove that the name is correct:-

// Select all bookings
$sql = "SELECT * FROM booking";

$result = mysqli_query($con,$sql);

echo "<p>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    print_r( $row );  // debug code

    // echo $row['custName'] . " - " . $row['roomNb'] . " - " . $row['date'];
    echo $row['custName'];
}
Friday, December 16, 2022
 
mkoryak
 
4
<?php
$sql = new mysqli('127.0.0.1','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');
?>

It will work. Just remove port from localhost (127.0.0.1)

Tuesday, August 30, 2022
 
5

You're not telling the JS how to send your POST parameters. Change your JS to:

data: { 'testscore':testscore },

This is the equivalent of "testscore=" + testcore in key=value form. It tells JS and PHP that you want the variable testscore to be mapped to the key "testscore", which you'll then pick up with $_POST['testscore']

Edit: See http://api.jquery.com/jQuery.ajax/, "Sending Data to the Server"

Wednesday, August 31, 2022
 
relaxxx
 
5

mysqli_connect("","username" ,"password","databasename");//Server name cannot be NULL

use loaclhost for server name(In Loacl)

<?php
    $con = mysqli_connect("localhost","username" ,"password","databasename");

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

Or can use MySQLi Procedural

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $con = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
?>

EDIT 01

$servername = "localhost";
$username = "root";
$password = "";
Monday, October 10, 2022
3

You're checking for the presence of $_POST['submit'], and setting some variables if it's found. You're then executing your mysql query outside the conditional block, so if the $_POST variables aren't found you'll get undefined variable errors.

Try

<?php
  $conn=mysql_connect("localhost","root","")
  or die("cannot connect");
    $db=mysql_select_db("ticket",$conn)
    or die("no db");
    if (isset($_POST['submit']))
    {
    $name1=$_POST['name'];
    $phone1=$_POST['phone'];
    $email1=$_POST['email'];
    $pass1=$_POST['password'];
    // This code now inside if block
    $query="insert into register(name,phone,email,pass) values ('$name1','$phone1','$email1','$pass1')";
    $result=mysql_query($query)
      or die("Error in pushing".mysql_error());
    mysql_close($conn);
  }
  ?>
Monday, November 28, 2022
 
w.c.
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :