Jump to content

I can't push my record to mysql database


JohnCooper
Go to solution Solved by Barand,

Recommended Posts

Here is my "index.php" code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset = "UTF-8">
        <meta name = "viewport" content="width=device-width,initial-scale=1.0">
        <title>Registration Form</title>
    </head>

    <h1>Blood Donation Camp </h1>
    <body bgcolor="FBB917">
        <div><h2>Registration Form</h2></div>
        <form action='connect.php' method="POST">
            <label for="user">Name:</label><br>
            <input type="text" name="name" id="name" required/> <br><br>
            
            <label for="email">Email:</label><br>
            <input type="email" name="email" id="email" required/> <br><br>

            <label for="phone">Phone:</label><br>
            <input type="text" name="phone" id="phone" required/> <br><br>

            <label for="bgroup">Blood Group:</label><br>
            <input type="text" name="bgroup" id="bgroup" required/> <br><br>

            <input type="submit" name="submit" id="submit">
        </form>
    </body>
</html>

Here is my "connect. php" code:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit']))
{
    $conn = mysqli_connect('localhost','root','','test1',3307) or die("Connection Failed!" .mysqli_connect_error());
    if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['bgroup']))
    {
        $name=$_POST['name'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $bgroup=$_POST['bgroup'];

        $sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')";

        $query = mysqli_query($conn,$sql);
        if($query)
        {
            echo "Entry Successful! <br>";
        }
        else 
        {
            echo "Error Occurred! <br>";
        }
    }
}
?>

 

I change my default port of mysql database as 3307 and also change default port of Apache Web Server as 81. But I think there is no connection error as "Connection failed!" message doesn't show up. Only "Error Occurred!" message is appeared!

Screenshot from 2023-02-06 02-42-44.png

Edited by JohnCooper
Link to comment
Share on other sites

  • Solution

Remove the single quotes from around the tablename and column name identifiers. Quotes are for string literals.

$sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')";
                  ^     ^  ^    ^ ^     ^ ^     ^ ^      ^
                             REMOVE

Also, you should use a prepared statement instead of putting user-provided variables into your query.

  • Like 1
Link to comment
Share on other sites

2 minutes ago, Barand said:

Remove the single quotes from around the tablename and column name identifiers. Quotes are for string literals.

$sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')";
                  ^     ^  ^    ^ ^     ^ ^     ^ ^      ^
                             REMOVE

Also, you should use a prepared statement instead of putting user-provided variables into your query.

Thanks,sir!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.