Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie: Error with PHP_SELF.

    Hi all,

    New to the forum and looking for some help with a site I'm working on. I'm new to PHP and working on a site that allows users to store details about a car in a MySQL database.

    It's in the very early stages at the moment, and the current page shows a list of the cars currently saved, with a form to add a new car. When a new car is entered, the 'Submit' button reloads the page with the updated list.

    The current situation is that "Error submitting car" is being displayed when I try to submit the form, but no specific error message is displayed despite the presence of 'mysqli_error()'. Any help figuring out why the form is not submitting correctly, and how to solve it, would be greatly appreciated. Let me know if you require any more information.


    <html>
    <head>
    <title>Today's Date</title>
    <body>
    <?php

    //Connect to database server
    $con = mysqli_connect("localhost", "root",

    "ang3lus", "cars");
    if (mysqli_connect_errno())
    {
    echo('<p>Unable to connect</p>' .

    mysqli_connect_error());
    }

    $result = mysqli_query($con, "SELECT * FROM CarTable");

    echo "<table border='1'>
    <tr>
    <th>Make</th>
    <th>Doors</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['CarMake'] . "</td>";
    echo "<td>" . $row['CarDoors'] . "</td>";
    echo "</tr>";
    }
    mysqli_close($con);

    ?>

    <form method="post" action="<?php echo $_SERVER

    ['PHP_SELF']; ?>">
    <p>Enter data:</p>
    <textarea name="carmake" rows="1" cols="25"

    wrap></textarea><br />
    <textarea name="cardoors" rows="1" cols="2"></textarea><br

    />
    <input type="submit" name="submitcar" value="SUBMIT" />
    </p>
    </form>

    <?php
    if (isset($_POST['submitcar'])) {
    $carmake = $_POST['carmake'];
    $cardoors = $_POST['cardoors'];
    $sql = "INSERT INTO CarTable SET
    CarMake='$carmake',
    CarDoors='$cardoors'";
    if (@mysqli_query($sql)) {
    echo('<p>Your car has been

    added.</p>');
    } else {
    echo('<p>Error submitting car: ' .

    mysqli_error() . '</p>');
    }
    }
    ?>
    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    It looks like you're closing your MySQL before you add the car below your form.

    mysqli_close($con);

    Close it after you add the car to the table.

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was honestly hoping that would solve it, but sadly it has not been that easy. I am still getting my customised error message when I hit submit, but still nothing from mysqli_error(). As this error is part of the last if/else statement, I can only assume it is something to do with mysqli_query($sql) not containing any data from POST, but I can't figure out why.

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    75
    Thanks
    1
    Thanked 2 Times in 2 Posts
    You set the MySQLi query up kind of weird. Normally it should be

    PHP Code:
    // I haven't a clue if you can change the $mysqli variable though.
    $mysqli = new mysqli("localhost""username""password""database");

    // Then add in your error checks.
    if($mysqli->connect_errno) {
    echo 
    "Error, failed to connect to database.";

    To query something you should use. $mysqli->query

    And mind I tell you, Fou said that the connect_errno was bugged til PHP version 5.3 so I don't know if it would effect you.

    Edit: Not to mention, that @ sign in front of your query. Is it suppose to be there?
    Last edited by xiong_ster123; 11-09-2013 at 05:42 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •