Forum Moderators: coopster
I have an kind of error that seems strange to me.
First I retrieve values from a mysql table to populate an array.
Second I use the indexes of that array to create a couple of checkboxes.
Later I use the selected checkboxes to send the selected values in an SET mysql column.
When I run the program, I get an error saying that the variables containing the number of rows ($num_rows) of the result set and the array variable($payment_means) itself are not defined(which i find strange).
Those 2 variables are located respectively on line 26 and 85 in my code editor.
To see what I am talking about, please, see the whole code below.
Can someone tell me where I am making an error?
Thanks, in advance:
<?php
$connection_id_1 = mysqli_connect('host_name', 'root', 'password', 'dbname');
$title = "Fill in the form to feed the expenses data";
session_start(); .
echo "<div style='text-align:center;'><pre>Welcome to this page: if you're not #*$!#*$!XX, please close it immediately".'<br></pre></div>';
if(!isset($_SESSION['name']))
{
echo "<p>Redirecting...</p>";
header('Location: [localhost...]
}
if(array_key_exists('submit', $_POST))
{
$date = $_POST['date'];
$date = mysqli_real_escape_string($connection_id_1, strip_tags(trim($date)));
$amount = $_POST['amount'];
$amount = mysqli_real_escape_string($connection_id_1, strip_tags(trim($amount)));
$payment_reason = $_POST['payment_reason'];
$payment_reason = mysqli_real_escape_string($connection_id_1, strip_tags(trim($payment_reason)));
$query = "SELECT * FROM payment_means ORDER BY means" ;
$result = mysqli_query($connection_id_1, $query ) or die("cannot execute query!");
$num_rows=mysqli_num_rows($result);
if($result)
{
$payment_means=array();
while(list($means) = mysqli_fetch_row($result))
{
$payment_means[]=$means;
}
}
$location = $_POST['location'];
$location = mysqli_real_escape_string($connection_id_1, strip_tags(trim($location)));
}
$mysql_date = "SELECT CURDATE()";
$date_result = mysqli_query($connection_id_1, $mysql_date) or die("No date retrieved from Mysql");
$row1 = mysqli_fetch_row($date_result);
?>
<html>
<head>
<style type="text/css">
label{color:gray; font-weight:bold; font-family:arial black; font-size:x-small;}
select{color:green; font-weight:bold;}
</style>
<title>
<?php echo $title; ?>
</title>
</head>
<body>
<?php
if(!isset($date) ¦¦ !isset($amount) ¦¦ !isset($payment_reason) ¦¦ !isset($payment_means) ¦¦ !isset($location))
{
echo "<div style='text-align:center;'>Please, provide values for the fields";
}
echo"
<div style='text-align:center;'><h4>Fill in the information below: </h4><br>
<form name = \"data_fill_in\" action = \"\" method = \"post\"><br><label>Date:<br>
<input type = \"text\" name = \"date\" maxlength = \"20\" value = \" $row1[0]\"></label><br><label>Expense amount:<br>
<input type = \"text\" name = \"amount\" maxlength = \"20\" ></label><br><label>Reason of Expense:<br>
<input type = \"text\" name = \"payment_reason\" maxlength = \"60\"></label><br>
<h5>Select your payment means below: </h5></div><br>";
?>
<?php
for($i=0; $i<=$num_rows; $i++)
{
echo "<input type=\"checkbox\" name=\"$payment_means[$i]\" value=\"".$payment_means[$i]."\">".$payment_means[$i]." ";
}
?>
<?php
echo '<div style="text-align:center;"><label>Location:<br>
<input type = "text" name = "location" size = "40"></label> <br>
<input type = "submit" name = "submit" value = "Submit"><br>
</form>'.'<br></div>' ;
?>
<?php
if(isset($date) AND isset($amount) AND isset($payment_reason) AND isset($payment_means) AND isset($location))
{
$query = "INSERT INTO expenses_duplicate VALUES (null, '$date', '$amount', '$payment_reason', '$payment_means', '$location')";
$result = mysqli_query($connection_id_1, $query);
if($result)
{
echo "Expense values successfully inserted, thank you.".'<br>';
sleep(5);
header('Location: [localhost...]
exit();
}
else
{
echo"<p style = 'color:red; font-weight:bold;'>Cannot insert the expense values, please try again.</p>".'<br>';
echo "$date, $amount, $payment_reason, $payment_means, $location".'<br>'; // This was not output.printf("Error Code: %d '<br>'", mysqli_errno($connection_id_1));
var_dump($result).'<br>'; // this is not output neither}
}
else
{
echo "<pre>No variable has been created yet</pre>";
}
?>
</body>
</html>