Forum Moderators: coopster
The whole code is intended to insert expenses in mysql. apart from some textboxes fields, i added checkboxes.
Here is the code:
<?php
// connection
$connection = mysql_connect('localhost', 'root', 'password');
// database
mysql_select_db('db_name');
// define what happens if the submit button is clicked.
// if(array_key_exists('submit', $_GET)). This is the same as the next line.
if(isset($_GET['submit']))
{
$date=mysqli_real_escape_string($connection, strip_tags(trim($_GET['date'])));
$expense_amount=mysqli_real_escape_string($connection, strip_tags(trim($_GET['expense_amount'])));
$expense_reason=mysqli_real_escape_string($connection, strip_tags(trim($_GET['expense_reason'])));
$location=mysqli_real_escape_string($connection, strip_tags(trim($_GET['location'])));
$payment_means=mysqli_real_escape_string($connection, strip_tags(trim($_GET['payment_means'])));
// check if payment means is array
if(is_array($payment_means)) $payment_means=implode(',', $payment_means);
// check values
if(!isset($date) ¦¦ !isset($expense_amount) ¦¦ !isset($expense_reason) ¦¦ !isset($location) ¦¦ !isset($payment_means))
{
echo "<pre>You didn't fill in all the fields</pre";
exit();
}
// prepare insert query
$query = "INSERT INTO expenses VALUES (null, '$date', '$expense_amount', '$expense_reason', '$payment_means', '$location')";
$result = mysqli_query($connection, $query);
if($result)
{
echo "Record correctly inserted<br>";
header('Location:http://localhost/mysql_queries/all_expenses.php');
}
else {echo "Query failed!"; exit();}
}
?>
<html>
<head>
<title>Process Expenses with Checkboxes</title>
<style type="text/css">
p,td{color:#666699; font-family:courier new; }
</style>
</head>
<body>
<?php
// here i include the form that the user will fill in
include('../expense_form.inc.php');
// the code of that include file is as follows. I just show it for completeness of your analysis.
<?php
// retrieve MySQL current date
$query = "SELECT CURDATE()";
$result = mysqli_query($connection, $query);
$row=mysqli_fetch_row($result);
echo "
<div style='text-align:center;'>
<p>Please, use the form below to insert expenses</p>
<form name=\"expense_form\" action=\"expense_checkbox.php\" method=\"GET\">
<table>
<tr>
<td style=' text_align:right;'>Date:</td>
<td><input type=\"text\" name=\"date\" value=\"$row[0]\" size=\"60\"></td>
</tr>
<tr>
<td style=' text_align:right;'>Expense Amount:</td>
<td><input type=\"text\" name=\"expense_amount\" size=\"60\"></td>
</tr>
<tr>
<td style=' text_align:right;'>Expense Reason:</td>
<td><input type=\"text\" name=\"expense_reason\" size=\"60\"></td>
</tr>
<tr><td colspan='2' style='text-align:center;'>Select one or more payment means below:</td></tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"bancontact\"></td>
<td>Bancontact</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"proton\"></td>
<td>Proton</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"visa card\"></td>
<td>Visa Card</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"cash\"></td>
<td>Cash</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"cash and proton\"></td>
<td>Cash and Proton</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"library card\"></td>
<td>Library Card</td>
</tr>
<tr>
<td style='text-align:right;'><input type=\"checkbox\" name=\"payment_means[]\" value=\"other\"></td>
<td>Other</td>
</tr>
<tr>
<td style=' text_align:right;'>Location:</td>
<td><input type=\"text\" name=\"location\" size=\"60\"></td>
</tr>
<tr>
<td colspan='2' style='text-align:center;'><input type='submit' name='submit 'value=\"submit\"></td>
</tr>
</table>
</form></div>
";
?>
?>
</body>
</html>
then in your destination script do this
echo '<pre>';
print_r($_POST);
echo '</pre>';
that should show you the whole POST array if it's there
also you don't need that long echo for your form, it just makes things more complicated having to escape every double quote. Do your php at the top and then drop out of php mode like so
<?php
// retrieve MySQL current date
$query = "SELECT CURDATE()";
$result = mysqli_query($connection, $query);
$row=mysqli_fetch_row($result);
?>
<div style='text-align:center;'>
<p>Please, use the form below to insert expenses</p>
<form name="expense_form" action="expense_checkbox.php" method="GET">
<table>
<tr>
<td style=' text_align:right;'>Date:</td>
<td><input type="text" name="date" value="$row[0]" size="60"></td>
</tr>
<tr>
<td style=' text_align:right;'>Expense Amount:</td>
<td><input type="text" name="expense_amount" size="60"></td>
</tr>
<tr>
<td style=' text_align:right;'>Expense Reason:</td>
<td><input type="text" name="expense_reason" size="60"></td>
</tr>
<tr><td colspan='2' style='text-align:center;'>Select one or more payment means below:</td></tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="bancontact"></td>
<td>Bancontact</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="proton"></td>
<td>Proton</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="visa card"></td>
<td>Visa Card</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="cash"></td>
<td>Cash</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="cash and proton"></td>
<td>Cash and Proton</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="library card"></td>
<td>Library Card</td>
</tr>
<tr>
<td style='text-align:right;'><input type="checkbox" name="payment_means[]" value="other"></td>
<td>Other</td>
</tr>
<tr>
<td style=' text_align:right;'>Location:</td>
<td><input type="text" name="location" size="60"></td>
</tr>
<tr>
<td colspan='2' style='text-align:center;'><input type='submit' name='submit 'value="submit"></td>
</tr>
</table>
</form></div>
</body>
</html>
2 observations:
When I fill in all the fields except the array "payment_means[]", I expected the script to exit at the level: if(!isset($date) ¦¦ !isset($expense_amount) ¦¦ !isset($expense_reason) ¦¦ !isset($location) ¦¦ !isset($payment_means))
{
echo "<pre>You didn't fill in all the fields</pre>";
exit();
}
However, the script continues at the "else" part of the INSERT query to display "Query failed" which shows that this time the script is alive, but a bit astonishing since i expected it to stop at the exit(); level, a bit higher in the script flow.
Second observation:
If I fill in all the fields and check some check boxes, I got the following error:
"Notice: Array to string conversion in C:\htdocs\MySQL_queries\expense_checkbox.php on line 16
Query failed"
Maybe here is where your intervention comes in again.
Best Regards and see you soon.
dbarasuk