PHP Form Processing Multiple Checkboxes - php

So i've got a table full of data. I have a page where i list all the contents of that able out through a while loop. Then the user can click a button on a record and it will generate an address label using the data it has and the generated label is done in a PDF. So that all works fine and dandy. However i was wondering is it possible to replace that button with an HTML Checkbox for each record to something like this
<input type="checkbox" name="<?php echo $recordTitle; ?>" />
<input type="hidden" name="<?php echo $recordID"; ?>" />
So this would allow people to select multiple records and then click one button and it would take all of those records and generate as many labels as it needed too. If this is possible, how would i process all of that, because i'd need to somehow loop through all the records the user has selected.
<?php
$eventSearch = $_POST['eventSearch'];
//Include Database connection
include '../includes/dbConnect.php';
foreach ($arr as &$value) {
$id = $value;
//Query Database for all customer info
$sql = "SELECT * FROM customerInterest WHERE `id` = '$value'";
$result = mysqli_query($dbLink, $sql);
$row = mysqli_fetch_assoc($result);
//require the document that creates the PDF
require('PDF_Label.php');
//Create a new PDF
$pdf = new PDF_Label('L7163');
$pdf->AddPage();
//assign the customer information to a variable
$sal = $row["salutation"];
$fn = $row["firstName"];
$ln = $row["lastName"];
$add1 = $row["contactAddress1"];
$add2 = $row["contactAddress2"];
$city = $row["contactAddressCity"];
$state = $row["contactAddressState"];
$post = $row["contactAddressPostcode"];
$country = $row["contactAddressCountry"];
if ($sal == "N/A") {
$sal = "";
}
if ($fn == "N/A") {
$fn = "";
}
if ($ln == "N/A") {
$ln = "";
}
if ($add1 == "N/A") {
$add1 = "";
}
if ($add2 == "N/A") {
$add2 = "";
}
if ($city == "N/A") {
$city = "";
}
if ($state == "N/A") {
$state = "";
}
if ($post == "N/A") {
$post = "";
}
if ($country == "N/A") {
$country = " ";
}
$add1 = $add1." ".$add2;
$post = str_replace(' ', '', $post);
$post = strtoupper($post);
$post = substr_replace($post, " ", 4, 0);
// Standard format
//create new page
// Print labels
$text = sprintf("%s\n%s\n%s\n%s %s",$sal." ".$fn." ".$ln, $add1, $city, $state, $post,$country);
$pdf->Add_Label($text);
}
//Output all the info
$pdf->Output();
?>
So the above contains the code i have now tried. I have posted the data to the PDf generating page (that is where all the code is from) and i keep getting the error (see below) and i cannot figure out why :/
Fatal error: Uncaught Error: Call to a member function Output() on null in /homepages/38/d735513801/htdocs/future/pdfGen/labelEX.php:80 Stack trace: #0 {main} thrown in /homepages/38/d735513801/htdocs/future/pdfGen/labelEX.php on line 80

Related

How to store same value repeatedly in sql? I'm using php, ajax & jquery

Question 1: I have multi line inputs(dynamically generated) which is storing the input value in MySql database. I have a input field which gets value only once and I want this value to be stored in the table repeatedly.
Question 2: How can I Increment date in dynamically generated field by one day using the user input as first date and pass the incremented date to my database?
LINK TO THE CODE
MY insert.php file:-
<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "crondiet");
if(isset($_POST["break_name"]))
{
$date_entry = $_POST["date_entry"];
$cid_code = $_POST["cid_code"];
$break_name = $_POST["break_name"];
$mid_meal = $_POST["mid_meal"];
$lunch_name = $_POST["lunch_name"];
$evening_snacks = $_POST["evening_snacks"];
$dinner_name = $_POST["dinner_name"];
print_r($date_entry);
$query = '';
for($count = 0; $count<count($break_name); $count++)
{
$date_entry_clean = mysqli_real_escape_string($connect, $date_entry[$count]);
$cid_code_clean = mysqli_real_escape_string($connect, $cid_code[$count]);
$break_name_clean = mysqli_real_escape_string($connect, $break_name[$count]);
$mid_meal_clean = mysqli_real_escape_string($connect, $mid_meal[$count]);
$lunch_name_clean = mysqli_real_escape_string($connect, $lunch_name[$count]);
$evening_snacks_clean = mysqli_real_escape_string($connect, $evening_snacks[$count]);
$dinner_name_clean = mysqli_real_escape_string($connect, $dinner_name[$count]);
if($date_entry_clean != '' &&$cid_code_clean != '' && $break_name_clean != ''&& $mid_meal_clean != '' && $lunch_name_clean != ''&& $evening_snacks_clean != '' && $dinner_name_clean != '')
{
$query .= '
INSERT INTO dietwithmoremeals(date_entry,cid_code,break_name,mid_meal,lunch_name,evening_snacks,dinner_name)
VALUES("'.$date_entry_clean.'","'.$cid_code_clean.'","'.$break_name_clean.'","'.$mid_meal_clean.'", "'.$lunch_name_clean.'","'.$evening_snacks_clean.'", "'.$dinner_name_clean.'");
';
}
}
if($query != '')
{
if(mysqli_multi_query($connect, $query))
{
echo 'Item Data Inserted';
}
else
{
echo 'Error';
}
}
else
{
echo 'All Fields are Required';
}
}
?>
In your query you can add one day to your inserted values:
$query .= '
INSERT INTO dietwithmoremeals(date_entry,cid_code,break_name,mid_meal,lunch_name,evening_snacks,dinner_name)
VALUES("'.$date_entry_clean.'" + INTERVAL 1 DAY,"'.$cid_code_clean.'","'.$break_name_clean.'","'.$mid_meal_clean.'", "'.$lunch_name_clean.'","'.$evening_snacks_clean.'", "'.$dinner_name_clean.'");
';

Not able to get proper value and print respective msg

Below is my code , I am trying to append values of $vid ,and than next time when my file loads i am trying to find currently passed value of $vid in array $ids, but every time i am getting success msg ,where i am going wrong, am i not able to append values properly ?
process.php
session_start();
if(isset($_POST)) {
$reason_data = trim($_POST['abuse_data']);
if($reason_data == "Video is offensive")
{
$reason = 'a:1:{i:0;s:18:"' . $reason_data .'";}';
}
else
{
$reason = 'a:1:{i:0;s:19:"' . $reason_data .'";}';
}
$motive = trim($_POST['remark_data']);
$uid = trim($_POST['abuse_uid']);
$vid = trim($_POST['abuse_vid']);
if(trim($_SESSION['abuse_vdo_id']) == '')
{
$_SESSION['abuse_vdo_id'] = $vid;
$str = $_SESSION['abuse_vdo_id'];
}
else {
$str = $str."," .$vid;
$_SESSION['abuse_vdo_id'] = $str;
}
print_r($_SESSION['abuse_vdo_id']);
echo "break";
print_r($str);
$ids = explode(',',$_SESSION['abuse_vdo_id']);
echo "break 2";
print_r($ids);
if(in_array($vid, $ids))
{
echo "already abused";
}
else
{
echo "success";
$query = "INSERT INTO ".DB_PREFIX."reports ( reason, motive, uid, vid ) VALUES ( '{$reason}','{$motive}', '{$uid}', '{$vid}' )";
$result = mysql_query($query);
}
}

PHP Comparing variables returns false every time

I have this script that checks a submitted form. It checks if all fields are all filled out, and checks if the user has submitted the form before. It also checks if the entered data is already in the database or not. When I try to check if the entered data is in the database, it always returns false. My question is: How can I efficiently check if the POST values are the same?
Code:
<?php
error_reporting(E_NOTICE ^ E_ALL);
$Name = $_POST['name'];
$ID = $_POST['id'];
$Topic_1 = $_POST['1'];
$Topic_2 = $_POST['2'];
$Topic_3 = $_POST['3'];
$Topic_4 = $_POST['4'];
$Topic_5 = $_POST['5'];
$Topic_6 = $_POST['6'];
$Topic_7 = $_POST['7'];
$Topic_8 = $_POST['8'];
$Topic_9 = $_POST['9'];
$Topic_10 = $_POST['10'];
$Topic_11 = $_POST['11'];
$Topic_12 = $_POST['12'];
$Topic_13 = $_POST['13'];
$Topic_14 = $_POST['14'];
$Topic_15 = $_POST['15'];
$IP = $_SERVER['REMOTE_ADDR'];
$Connect = new mysqli("127.0.0.1", "root", "", "Data");
$Check = 'SELECT * FROM Submissions WHERE School_ID = "'.$ID.'" AND IP = "'.$IP.'"';
$Insert = 'INSERT INTO Submissions (Name, School_ID, Topic_1, Topic_2, Topic_3, Topic_4, Topic_5, Topic_6, Topic_7, Topic_8, Topic_9, Topic_10, Topic_11, Topic_12, Topic_13, Topic_14, Topic_15, IP) VALUES ("'.$Name.'", "'.$ID.'", "'.$Topic_1.'", "'.$Topic_2.'", "'.$Topic_3.'", "'.$Topic_4.'", "'.$Topic_5.'", "'.$Topic_6.'", "'.$Topic_7.'", "'.$Topic_8.'", "'.$Topic_9.'", "'.$Topic_10.'", "'.$Topic_11.'", "'.$Topic_12.'", "'.$Topic_13.'", "'.$Topic_14.'", "'.$Topic_15.'", "'.$IP.'")';
if($Name && $ID != "")
{
if($Result = $Connect->query($Check))
{
$Rows = $Result->num_rows;
if($Rows == 0)
{
if($_POST != $_POST)
{
if($Go = $Connect->prepare($Insert))
{
if($Go->execute())
{
echo 'Thanks';
}
else
{
echo 'There Was An Error';
}
}
else
{
echo 'There Was An Error';
}
}
else
{
echo 'No Two Values Can Match.';
}
}
else
{
echo 'You Cant Vote Twice.';
}
$Result->close();
}
else
{
echo 'There Was An Error.';
}
}
else
{
echo 'Please Fill Out All Fields';
}
$Connect->close();
Your if statement should look like
if($name != "" && $check != "")
Here's the error:
if($_POST != $_POST)
You do probably want to compare the result from the db with the $_POST instead.
$Row = $Result->fetch_assoc();
if($Row != $_POST)
Prior to doing a comparison use var_dump() on the variables to check what they actually contain.
var_dump($Name);
var_dump($ID);
exit();
Then check for a negative or positive match.
if( !empty($Name) && empty($ID) ){
exit('ah, name filled in but not id ...');
}
You can even spoof that in a separate file.
<?php
$Name = 'Bob';
$ID = ''; // or use 0 or any test you want
var_dump($Name);
var_dump($ID);
if( !empty($Name) && empty($ID) ){
exit('ah, name filled in but not id ...');
}
Isolating problems like this will help you develop incrementally, get something working, then add more lines till you arrive at your destination.
To check if not two POST values are the same:
array_diff($_POST, array_unique($_POST));
What you looking for is following
$_POST['1'] = 'a';
$_POST['2'] = 'b';
$_POST['3'] = 'c';
$_POST['4'] = 'a';
$_POST['5'] = 'd';
$results = array_unique($_POST);
var_dump($results);
returns:
array
1 => string 'a' (length=1)
2 => string 'b' (length=1)
3 => string 'c' (length=1)
5 => string 'd' (length=1)
You can't really so easily check if a person did submit a form before.
One way is to add one more hidden field to form if the request came with POST.
Something like that:
<form method="POST" action="">
<?php
if(isset($_POST['submit'])) {
echo '<input type="hidden" name="second_post" value="1">';
} ?>
<!-- Other form items -->
<input type="submit" name="submit" value="1">
</form>
Then you can check is it a second time with:
if(isset($_POST['second_post'])) {
// Second time of form post;
} else {
// First (or zero) time post.
}

How to make form values remain in form

I'm developing a simple PHP website. I have a form for updating values. When that form is accessed via PHP(website) , It should have previous values, But it hasn't. How to do that ?
Because, Otherwise, It shows all fields empty and when one or two are updated, Other gets updated as empty. Please help !
<?php
require_once('../includes/config.php');
require_once('../includes/functions.php');
require_once('../includes/session.php');
require_once('../includes/database.php');
require_once('../includes/user.php');
require_once('../includes/photograph.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>
<?php
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('lives_in', 'belongs_to', 'college', 'works_at', 'grade', 'grade2', 'hobbies', 'zodiac', 'phone_no', 'facebook', 'company', 'bio');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$lives_in = trim($database->escape_value($_POST['lives_in']));
$belongs_to = trim($database->escape_value($_POST['belongs_to']));
$college = trim($database->escape_value($_POST['college']));
$works_at = trim($database->escape_value($_POST['works_at']));
$grade = trim($database->escape_value($_POST['grade']));
$grade2 = trim($database->escape_value($_POST['grade2']));
$hobbies = trim($database->escape_value($_POST['hobbies']));
$zodiac = trim($database->escape_value($_POST['zodiac']));
$phone_no = trim($database->escape_value($_POST['phone_no']));
$facebook = trim($database->escape_value($_POST['facebook']));
$company = trim($database->escape_value($_POST['company']));
$bio = trim($database->escape_value($_POST['bio']));
if (empty($errors) ) {
$query = "UPDATE users
SET lives_in = '{$lives_in}',
belongs_to = '{$belongs_to}',
college = '{$college}' ,
works_at = '{$works_at}',
grade = '{$grade}',
grade2 = '{$grade2}',
hobbies = '{$hobbies}',
zodiac = '{$zodiac}',
phone_no = '{$phone_no}',
facebook = '{$facebook}',
company = '{$company}',
bio = '{$bio}'
WHERE id = 23
LIMIT 1";
$result = mysql_query($query, $database->connection);
if ($result) {
$message = "Account Updated";
} else {
$message = mysql_error();
}
} else {
if (count($errors) == 1) {
$message = mysql_error();;
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
} else { // Form has not been submitted.
$lives_in = "";
$belongs_to = "";
$college = "";
$works_at = "";
$grade = "";
$grade2 = "";
$hobbies = "";
$zodiac = "";
$phone_no = "";
$facebook = "";
$company = "";
$bio = "";
}
?>
It's very easy, just print out the previous values in the form when you create it with php.
For example:
<input type="text" name="field_name" value="<?php echo $previous_value;?>" />
Or, if it's a select, let's say:
<select name="field_name">
<?php foreach($posible_values as $value){ ?>
<option value="<?php echo $value;?>"
<?php echo ($value==$previous_value)?'selected="selected"':'';?> >
<?echo $value?>
</option>
<?php } ?>
</select>

web Development, PHP display variable from SQL as something else

Ok So i have an PHP page and i have a database.
In my database i have a table with a Field one of the fields is called accounttype it is enum('n', 'm', 's')
I am trying to display on my PHP page if the user is N it should say Normal User if the user is E Expert user or S Super user...
How do i go about doing this?
Top of PHP Page
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
?>
Where i am trying to display on the page This is the code. Right now it just puts a blank space.
<span class="admin">Edward</span>
<span class="date">March 19, 2048</span>
<span class="tag"><?php echo "$accounttype"; ?></span>
<span class="comment">166 comments</span>
picture
http://i.stack.imgur.com/KXu9A.png
first make a connection, than dont make a while, make a if like this
if($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
and than
$speaking_type = null;
switch($accounttype) {
case 'n':
$speaking_type = 'Normal User';
break;
case 'm':
$speaking_type = 'Expert User';
break;
case 's':
$speaking_type = 'Super User';
break;
default:
$speagink_type = 'none';
//throw new Exception('unsupported account type '.$accounttype);
}
echo $speaking_type;
I think the problem is your scope. Your variables are defined within the while-loop, and so they are unknown further in the document. Try instantiating them on top (before the while-loop) like this:
$phone = null;
$country = null;
$state = null;
$city = null;
$accounttype = null;
$bio = null;
Than the variables will be known outside the while and the values will be remembered when you print them.
I thought u didn't connect to the database first .use following code to the connect with your credentials.That's why you are seeing a blank space
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);

Categories