Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't insert new records through PHP into mysql database

Tags:

php

mysql

When I press the submit button I get error.

object not found error.

And the page automatically adds empty entries with auto incremented primary key (without pressing the submit button).

I am still a beginner in PHP, I searched thoroughly but I can't find out what's wrong in code.

  <html>   
<head>
    <title>Add New Record in MySQL Database</title>
</head>   
<body>
    <form action="insert.php" method="post">
        <p>

            <label for="Name">Full Name:</label>

            <input type="text" name="Name" id="Name">

        </p>
        <p>
            <label for="Code">Code:</label>
            <input type="text" name="Code" id="Code">
        </p>
        <p>
            <label for="GPA">GPA:</label>
            <input type="text" name="GPA" id="GPA">
        </p>
        <input type="submit" value="Submit">
    </form>

    <?php
 /* Attempt MySQL server connection. Assuming you are running MySQL

 server with default setting (user 'root' with no password) */

 $link = mysqli_connect("localhost", "username", "password", "students");
// Check connection

if ($link === false) {

die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$full_name = filter_input(INPUT_POST, 'full_name');
$code = filter_input(INPUT_POST, 'code');
$gpa = filter_input(INPUT_POST, 'gpa');
// attempt insert query execution
$sql = "INSERT INTO info VALUES ('$full_name', '$code', '$gpa')";
if (mysqli_query($link, $sql)) {
 echo "Records added successfully. $full_name";
} else {
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
</body>
</html>  
like image 852
Tasneem Salah Avatar asked Oct 02 '16 16:10

Tasneem Salah


People also ask

How do I add a record to a MySQL database?

When inserting a single row into the MySQL table, the syntax is as follows: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row.

Can I use PHP to connect to MySQL database?

PHP 5 and later can work with a MySQL database using: MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects)

How do I add a record in phpmyadmin?

To add records inside a database table, open the table with phpMyAdmin and click on the Insert tab. Enter the desired data in the corresponding fields and click on Go to store it. You can see the newly inserted record by clicking on the Browse tab.


1 Answers

Try this:

$full_name = filter_input(INPUT_POST, 'Name');
$code = filter_input(INPUT_POST, 'Code');
$gpa = filter_input(INPUT_POST, 'GPA');

The reason why I wrote that is because your input names contain Name, Code and GPA so you need to write this exactly as your input names (case-sensitive).

like image 107
Gytis TG Avatar answered Oct 14 '22 10:10

Gytis TG