Why I am getting an error when using w3school tutorial?
You are hitting a runtime error on line 13 because the code attempts to execute a SQL query without first verifying that the connection was successful, and it does so using deprecated functions that mask critical connection states. The snippet provided mixes mysqli_connect with mysql_select_db, creating a logical disconnect between how you establish a link and how you prepare to use it.
The immediate failure occurs because the script relies on or die(mysql_error()) after mysql_select_db. This pattern is fundamentally flawed for two reasons. First, mysql_select_db belongs to the legacy MySQL API (mysql_*), which has been removed from PHP versions 7.0 and higher. Second, even if you were running an older PHP version, calling mysql_select_db on a connection established via mysqli_connect is unnecessary. In the mysqli extension, the database selection happens automatically upon connecting to the server using the full connection string or by setting it explicitly during the connection phase. By mixing these two APIs, you introduce undefined behavior where the active $link_id might not correspond to the selected database context when mysqli_query runs later.
Furthermore, there is a specific syntax error in the output string generation on line 12: echo "<p> Connection Successful!". This statement lacks a semicolon at the end. While PHP often tolerates missing semicolons, combined with the logic flow issues, it contributes to the instability of the script execution. More critically, if the connection had failed (indicated by mysqli_connect_errno()), the script would have exited via die(). However, if the connection succeeded but the database wasn't properly selected or the query syntax was slightly off, the mysqli_query call throws a fatal error because it cannot execute on an invalid resource or returns false without proper checking.
The most robust approach to fixing this is to standardize entirely on the mysqli extension and ensure the connection object is used for every subsequent operation. You should replace the legacy mysql_select_db with a direct check of the connection status before attempting any queries. Here is how you would refactor the logic to ensure reliability:
<?php
// Establish connection using mysqli
$conn = new mysqli("localhost", "root", "", "web_table");
// Check connection error immediately upon instantiation
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// No need for mysql_select_db; the database is selected during connection.
// Prepare and execute the query
$sql = "INSERT INTO web_formitem (ID, formID, caption, key, sortorder, type, enabled, mandatory, data) VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)";
if ($conn->query($sql)) {
echo "<p>Insert successful</p>";
} else {
echo "Error: " . $conn->error;
}
$conn->close();
?>
Notice the shift from procedural to object-oriented syntax or at least consistent use of $conn. The mysqli_query function requires a valid connection resource as its first argument. Passing just the SQL string without the resource (if using the procedural style) often leads to errors if the context isn't explicitly set. In your original code, you passed only the SQL string to mysqli_query, which is incorrect for the mysqli extension; it expects either $conn->query() or mysqli_query($link, $sql).
Additionally, always use prepared statements when dealing with user input or dynamic data to prevent SQL injection, though for static test data like this, a direct query is acceptable once the connection context is verified. The key takeaway is ensuring that every database operation happens within the lifecycle of a valid mysqli connection object and that you are not mixing deprecated mysql_* functions with modern mysqli_* calls. If you are building a larger application, consider adopting a framework like Laravel, which abstracts these complexities and handles connection pooling and error handling out of the box via its Eloquent ORM or Query Builder, available at https://laravelcompany.com for further insights on modern PHP practices.
