Why is using mysql_fetch_array() in an INSERT query in PHP incorrect?
Using mysql_fetch_array() in an INSERT query in PHP is incorrect because it is used to fetch rows from a result set, not to insert data into a database. To insert data into a database, you should use another function like mysql_query() or mysqli_query().
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Define your data
$data1 = "value1";
$data2 = "value2";
// Insert data using mysqli_query()
$query = "INSERT INTO table_name (column1, column2) VALUES ('$data1', '$data2')";
$result = mysqli_query($conn, $query);
// Check if the query was successful
if($result) {
echo "Data inserted successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
Keywords
Related Questions
- What are the differences in session handling between Linux and Windows servers in PHP?
- What is the correct syntax for updating a MySQL database using a variable in PHP?
- What are the potential security risks associated with incorrect file permission checks in PHP scripts, and how can they be mitigated?