-1
<?php 

    /*
        Our "config.inc.php" file connects to database every time we include or require
        it within a php script.  Since we want this script to add a new user to our db,
        we will be talking with our database, and therefore,
        let's require the connection to happen:
    */
    require("config.inc.php");

    if(!empty($_POST))
    {
        echo "Posted data not empty";
    } else {
        $query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";

        $query_params = array(
            ':name' => $_POST['userName'],
            ':contact' => $_POST['contactNumber'],
            ':address' => $_POST['userAddress'],
            ':store' => $_POST['storeList'],
            ':request' => $_POST['requestBody']
        );

        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute($query_params);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());

            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error2. Please Try Again!";
            die(json_encode($response));
        }

        $response["success"] = 1;
        $response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
        echo json_encode($response);
?>
    <h1>Register</h1> 
    <form action="register.php" method="POST"> 
        Name:<br /> 
        <input type="text" name="userName" value="" /> 
        <br /><br /> 
        Contact Number:<br /> 
        <input type="text" name="contactNumber" value="" /> 
        <br /><br /> 
        Address:<br /> 
        <input type="text" name="userAddress" value="" /> 
        <br /><br />
        Store:<br /> 
        <input type="text" name="storeList" value="" /> 
        <br /><br /> 
        Request:<br /> 
        <input type="text" name="requestBody" value="" /> 
        <br /><br /> 
        <input type="submit" value="Call A Rocket" /> 
    </form>
<?php
    }
?>

Notice: Undefined index: userName in C:\xampp\htdocs\callarocket\register.php on line 20

Notice: Undefined index: contactNumber in C:\xampp\htdocs\callarocket\register.php on line 21

Notice: Undefined index: userAddress in C:\xampp\htdocs\callarocket\register.php on line 22

Notice: Undefined index: storeList in C:\xampp\htdocs\callarocket\register.php on line 23

Notice: Undefined index: requestBody in C:\xampp\htdocs\callarocket\register.php on line 24 {"success":0,"message":"Database Error2. Please Try Again!"}

May I know what's wrong with this code ?

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
JayJay
  • 11
  • 1

1 Answers1

1

So, you are checking if $_POST is not empty and if it is, you use the variables that are not there to create a query. You should put your sql code in the part where it says $_POST is not empty.

You should however check all keys if they are present. That way, you are sure no errors will occur.

<?php 
    require("config.inc.php");

    if(!empty($_POST))
    {
        echo "Posted data not empty";
        $query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";


        $query_params = array(
            ':name' => $_POST['userName'],
            ':contact' => $_POST['contactNumber'],
            ':address' => $_POST['userAddress'],
            ':store' => $_POST['storeList'],
            ':request' => $_POST['requestBody']
        );


        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute($query_params);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());

            //or just use this use this one:
            $response["success"] = 0;
            $response["message"] = "Database Error2. Please Try Again!";
            die(json_encode($response));
        }

        $response["success"] = 1;
        $response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
        echo json_encode($response);
    }else{
?>

    <h1>Register</h1> 
    <form action="register.php" method="POST"> 
        Name:<br /> 
        <input type="text" name="userName" value="" /> 
        <br /><br /> 
        Contact Number:<br /> 
        <input type="text" name="contactNumber" value="" /> 
        <br /><br /> 
        Address:<br /> 
        <input type="text" name="userAddress" value="" /> 
        <br /><br />
        Store:<br /> 
        <input type="text" name="storeList" value="" /> 
        <br /><br /> 
        Request:<br /> 
        <input type="text" name="requestBody" value="" /> 
        <br /><br /> 
        <input type="submit" value="Call A Rocket" /> 
    </form>
    <?php
    }
    ?>
Jerodev
  • 32,252
  • 11
  • 87
  • 108