Reputation: 15
Im working on a php script that adds data to a mysql table there are 4 fields all of which i would like required, I think i got the majory of the code correct being that i copied the code from a php book then made some changes to reflect what i was looking for for my particular srcipt but when i go to test the submit function it just gives me a white screen instead of a success/error message, im kinda stumped as to where to go from here. below is a copy of my code.
<html>
<head>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//input validation all required
if (!empty($_POST['airport_name'])) {
$an = trim($_POST['airport_name']);
if (!empty($_POST['airport_code'])){
$ac = trim($_POST['airport_code']);
if (!empty($_POST['airport_lat'])){
$alat = trim($_POST['airport_lat']);
if (!empty($_POST['airport_long'])){
$along = trim($_POST['airport_long']);
//if (!empty($_POST['airport_name'])){
//$an = trip($_POST['airport_name']);
require('/../mysqli_connect.php');
$q = 'INSERT INTO airports (airport_name, airport_code, airport_lat, airport_long) VALUES (?, ?, ?, ?)';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'ssss', $an, $ac, $alat, $along);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_affected_rows($stmt) == 1) {
echo '<p>The airport has been added!</p>';
$_POST = array();
} else {
$error = 'The new airport could not be added to the database!';
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
} //if airport lat
else{
$error = 'Please enter airport lat';
}
} //if airport lat
else{
$error = 'Please enter airport latitude';
}
} //if airport code
else {
$error = 'Please enter an airport code';
}
} //if airport name
else {
$error = 'Please enter an airport name';
}
} // end of submission if
if (isset($error)) {
echo '<h1>Error!</h1>
<p stlye="font-weight: bold; color: #COO">'. $error . 'Please try again</p>';
}
?>
<h1>Add Airport</h1>
<form action="add_apt.php" method="post">
<fieldset><legend>Fill out the for to and and airpot:</legend>
<p><b>Airport Name:</b><input type="text" name="airport_name" size="10" value="<?php if (isset($_POST['airport_name'])) echo $_POST['airport_name']; ?>"/></p>
<p><b>Airport Code:</b><input type="text" name="airport_code" size="4" maxlegnth="4" value="<?php if (isset($_POST['airport_code'])) echo $_POST['airport_code']; ?>"/></p>
<p><b>Airport Lat:</b><input type="text" name="airport_lat" size="10" maxlegnth="40" value="<?php if (isset($_POST['airport_lat'])) echo $_POST['airport_lat']; ?>"/></p>
<p><b>Airport Long:</b><input type="text" name="airport_long" size="10" maxlegnth="40" value="<?php if (isset($_POST['airport_long'])) echo $_POST['airport_Long']; ?>"/></p>
</fieldset>
<div align="center"><input type="submit" name="submit" Value="Add Airport"/></div>
</form>
</body>
</html>
Here are the errors i get when i emabled errors Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 29
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 30
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 31
Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 33
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 39
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home5/virtua15/public_html/gatewayaviation/add_apt.php on line 40
Mysqli_connect.php script
<?php
DEFINE ('DB_USER', '******_******');
DEFINE ('DB_PASSWORD', '*******');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '******_gateway');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
Upvotes: 0
Views: 428
Reputation: 6005
There is a simple way to find what the error is, even without using display_errors. In the part of the script where you are building and executing your query, you should change it to the following:
if ($stmt = mysqli_prepare($dbc, $q)) {
mysqli_stmt_bind_param($stmt, 'ssss', $an, $ac, $alat, $along);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_affected_rows($stmt) == 1) {
echo '<p>The airport has been added!</p>';
$_POST = array();
} else {
$error = 'The new airport could not be added to the database!';
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
else {
echo '<p>'.mysqli_error($dbc).'</p>';
}
By doing this, you are getting any MySQL errors that are occurring in your query. In this case, the error being generated is:
Column count doesn't match value count at row 1
Looking closer at the code, this error is occurring because you are trying to add 4 fields (airport_name, airport_code, airport_lat, airport_long)
, but you have 5 placeholders in your SQL statement (?, ?, ?, ?, ?)
, so remove one of the placeholders and it will now work.
EDIT: In response to the comments in your question and this answer, also check that the connection file is being correctly included in your main script.
Upvotes: 1