echo with attribute name for query purposes php pdo - php

<form method="POST" action="include/crud.php" enctype="multipart/form-data" >
<?php
foreach (LoadAnouncements() as $value){
/*echo "<div id='bb'></div>";*/
echo "<hr/>";
echo $value['searchresultwhat'];
echo "<br/>\n";
echo $value['searchresultwhen'];
echo "<br/>\n";
echo $value['searchresultwhere'];
echo "<hr/>";
/*echo "<div id='bb'></div>";*/
}
?>
</form>
I have this form that show an echo is there a way that I can add the attribute name in this echo so I can use it to query the database? I search for ideas if putting attribute to echo is possible but I haven't found anything yet any suggestion is appreciated

You are looking it?
<input type="text" name="searchresultwhat[]" value="<?php echo $value['searchresultwhat']; ?>" />
<input type="text" name="searchresultwhen[]" value="<?php echo $value['searchresultwhen']; ?>" />
<input type="text" name="searchresultwhere[]" value="<?php echo $value['searchresultwhere']; ?>" />
Edited
<?php
echo '<table>';
echo '<tr><th>What </th><th> When</th><th> Where</th><tr>';
foreach (LoadAnouncements() as $value){
echo "<tr><td>".$value['searchresultwhat']."</td>
<td>"$value['searchresultwhen']."</td>
<td>".$value['searchresultwhere']."</td>
</tr>";
}
echo '</table>';
?>

Do you want to add attribute to certain value in PHP? But you can't do it because $value['searchresultwhat'] and others of the kind are simple strings - as is! They can't send separated data (or attributes) with themself.
EDIT:
You can send seprated data about certain value using array:
$value['searchresultwhat'] = array("name", "value");
$value['searchresultwhen'] = array("name2", "2014-09-01");
$value['searchresultwhere'] = array("name3", "kittens");
echo $value['searchresultwhat'][0] . " is " . $value['searchresultwhat'][1];
echo $value['searchresultwhen'][0] . " = " . $value['searchresultwhen'][1];
echo $value['searchresultwhere'][0] . " has " . $value['searchresultwhere'][1];

Related

PHP POST yields empty result using sqlite3 database

I have a simple web form in HTML using POST and PHP against an SQLite3 database. The form asks for a database id. When entered and hitting submit, the result does not output to the screen.
Here is the code. Please help! It appears the variable is empty. Where am I going wrong?
Original Form HTML (edit_entry1.html):
<body bgcolor = "#C7CFCA">
</p></p>
<center><h2>Update a Record<br>
<form method="POST" action="update_record.php">
<br />
<center>
<h3>To update a record click on 'View Database' and find the record ID you want to update and enter that ID here.</h3>
</center>
<table>
<tr><td><h2>Record ID: </td><td><h2><input style="font-size:20px" type="text" name="archivo" size="80"></td></tr>
<tr><td><input type="submit" name="save" value="Submit" style="font-size:20px"></td><td><input type=reset value="Reset Form" style="font-size:20px"></td>
</table>
</form>
</center>
</html>
This is the corresponding php script (update_record.php):
<?php
{
//open the database
$db = new SQLite3('wc.db');
// Set Variables from POST
$record = $_POST["archivo"];
//now output the data to a simple html table...
echo "<!DOCTYPE html>\n";
echo "<html lang=\"en\">\n";
echo "<body bgcolor = \"#C7CFCA\" text = \"black\">\n";
echo "<center>";
echo "<p>Record ID is <?php echo $record ?>.</p>";
echo "<table>\n";
echo "<h2>Update a Record</h2>";
echo "<tr><th><u><h3>ID</th><th><u><h3>Last Name</th><th><u><h3>First Name</th>";
echo "<th><u><h3>Middle Name</th><th><u><h3>Section</th>";
echo "<th><u><h3>Lot</th><th><u><h3>Plot</th><th><u><h3>Burial Date</th><th><u><h3>Veteran</th></tr>\n";
$results = $db->query('SELECT id,last_name,first_name,middle_initial,section,lot,plot,burial_date,veteran FROM burials WHERE id = $record');
while ($row = $results->fetchArray()) {
echo "<tr><td><center><h3>" . $row['id'] . "</td><td><center><h3>" . $row['last_name'] . "</td><td><center><h3>" .
$row['first_name'] . "</td><td><center><h3>" . $row['middle_initial'] . "</td><td><center><h3>" .
$row['section'] . "</td><td><center><h3>" . $row['lot'] . "</td><td><center><h3>" . $row['plot'] . "</td><td><center><h3>" . $row['burial_date'] . "</td><td><center><h3>" . $row['veteran'] . "</td></tr>\n";
}
echo "</table>\n";
echo "<p>Record ID is <?php echo $record ?>.</p>";
echo "<label for=\"sql\"><h3>What do you want to update? </label>";
echo "<select id=\"option\">";
echo "<h3><option value=\"last_name\"><h3>Last Name</option>";
echo "<option value=\"fist_name\"><h3>First Name</option>";
echo "<option value=\"middle_initial\"><h3>Middle Name</option>";
echo "<option value=\"section\"><h3>Section</option>";
echo "<option value=\"lot\"><h3>Lot</option>";
echo "<option value=\"plot\"><h3>Plot</option>";
echo "<option value=\"burial_date\"><h3>Burial Date</option>";
echo "<option value=\"veteran\"><h3>Veteran Status</option>";
echo "</select>";
echo "<h2><input style=\"font-size:15px\" type=\"text\" name=\"opt\" size=\"30\">";
echo "</body>\n";
echo "</html>";
}
?>
When I put, say, 1 as the record id in the form, nothing is outputted. I'm new to this and would definitely appreciate some pointers/tips.
To prevent a SQL injection attack, you should consider using the prepare/bind/execute pattern. Use example 1 in the SQLITE3::prepare doc as a guide.
Regarding the problem at hand: From the PHP: Strings doc:
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
Since the SQL query is enclosed in single-quotes ('), the $record variable is not parsed. In other words, what you see is what is being sent to the database, thus no rows are returned.

How to get value from input with data from database

I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}

How do I create an html form with multiple checkboxes

This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value

How to fetch POST values of a dynamically generated form in php

this is my dynamically generated form that allows the user to input scores of students.my challenge is how to capture the specific subjectCode and its score because am using the subjectcode to store in the database .
<?php
$attributes=array('class'=>'','id'=>'');
echo form_open('scoreInsertion/insertScores',$attributes);
echo "<table>";
echo "<tr>";
echo "<td>";?>
<label for="admNo>">Admission Number<span class="required">*</span></label>
<?php echo form_error('admNo');
echo "</td>";
echo "<td>";?>
<input type="text" name="admNo" id="admNo" value="<?php echo set_value('admNo')?>" />
<?php echo "</tr>";
echo "<tr>";
echo "<td>";
?>
<label for="studentName>">Student Name<span class="required">*</span></label>
<?php echo form_error('studentName');
echo "</td>";
echo "<td>";?>
<input type="text" name="studentName" id="studentName" value="<?php echo $studentName?>" />
<?php echo "</tr>";
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="examCategory">Exam Category<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="examCategory">
<option value="">--Select Category--</option>
<?php
print_r($exams);
if(isset($exams) && is_array($exams)){
foreach($exams as $exam){
$examId=$exam->id;
$examName=$exam->category;
echo "<option value='$examId'>$examName</option>";
}
}
?>
</select></td><?php
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="term">Academic Term<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="term">
<option value="">--Academic Term--</option>
<?php
if(isset($terms) && is_array($terms)){
foreach($terms as $term){
$termId=$term->termId;
$termName=$term->name;
echo "<option value='$termId'>$termName</option>";
}
}
?>
</select></td>
<?php
echo "<tr>";
//loop through the subjects array and display them on the view
if(isset($subjects) && is_array($subjects)){
foreach($subjects as $subject){
$subjectName=$subject->subName;
$subjectCode=$subject->subCode;
echo "<tr>";
echo "<tr></tr>";
echo "<td>";?>
<label for="<?php echo $subjectName?>"><?php echo $subjectName;?><span class="required">*</span></label>
<?php echo form_error($subjectName);
echo "</td>";
echo "<td>";?>
<input type="text" name="<?php echo $subjectCode?>" id="<?php echo $subjectName?>" value="<?php echo set_value($subjectCode)?>" />
<?php echo "</tr>";
}
}
?>
<?php echo "<tr>";echo "<td>";
echo form_submit('submit','Submit');
echo "</td>";
echo "</tr>";
?>
</p>
<?php
echo "</table>";
echo form_close();
?>
PHP script to process the above POST details is below:
function insertScores(){
$admNo=$this->input->post('admNo');
$term=$this->input->post('term');
//get the post variables from the Form
$posted = $this->input->post();
$x = array_keys($posted);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
echo "<br>";
//write the sql
$form_data=array(
'admNo' =>$admNo,
'subCode'=>$y,
'termId'=>$term,
'score'=>$_POST[$y]
);
print_r($form_data);
$this->SaveForm($form_data);
}
}
If that really has to be, you could set a specific prefix for the name attribute and then look for a key starting with that prefix among your $_POST keys. Something like that:
<input type="text"
name="subject_<?php echo $subjectCode?>"
id="<?php echo $subjectName?>"
value="<?php echo set_value($subjectCode)?>" />
Then search for it in the $_POST array.
$subjectCode = null;
foreach (array_keys($_POST) as $index) {
if (strpos($index, 'subject_') === 0) { // you found the needed element
// get its value and save it in a variable
$subjectCode = $_POST[$index];
break;
}
}
This code solved my problem, I fetch the subCode using admNo in the db then compare with the POST names.
$subjects=$this->subjectModel->get_subjects();
foreach($subjects as $subject){
$subCode= $subject->subCode;
foreach($_POST as $key => $value){
//print_r($key);
//print_r($value);
$form_data=array();
if($key == $subCode){
//echo $subCode. "=". $value;
//write the sql for insertion into the database
$form_data = array(
'admNo'=>$admNo,
'subCode'=>$subCode,
'termId'=>$term,
'formId'=>$formstudy,
'score'=>$value,
'year'=>$year
);
$this->SaveForm($form_data);
}
}
}

PHP and Session error

Hello I have a problem with a session. When I use a session to pass a variable to another page the values of that variable always still the same in the other page. No matter what row I selected. When I change the "action" to the same page where the variable is, the value shows correct. Sorry for my bad English if someone speak Spanish let my know to explain better. I really need help in this.
Here is my code:
<?php
include_once 'rnheader.php';
session_start();
$ticket_select = $_POST['serviceID'];
echo ' Create Service ';
echo '<table border="1" >';
echo '<tr>';
echo '<th>Service ID</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Notes</th>';
echo '<th>Submit By</th>';
echo '<th>Assigned Employee</th>';
echo '<th>Assigned Group</th>';
echo '<th>Category</th>';
echo '<th>Status</th>';
echo '<th>Urgency</th>';
echo '<th>Customer</th>';
echo '<th>Day Created</th>';
echo '</tr>';
$query = ("SELECT ServiceID, Title, Description, Notes, SubmitBy, AssignedEmp, " .
"AssignedGroup, NameCategory, TipoStatus, TiposUrgencia, CustomerName, DayCreation FROM Service");
$result = queryMysql($query);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td><form method ="post" action="rnservices1.php">';
?>
<input type="submit" name="serviceID" value=<?php echo $row['ServiceID']?>
<?php
echo '</form>';
echo '<td>'.$row['Title'].'</td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Notes'].'</td>';
echo '<td>'.$row['SubmitBy'].'</td>';
echo '<td>'.$row['AssignedEmp'].'</td>';
echo '<td>'.$row['AssignedGroup'].'</td>';
echo '<td>'.$row['NameCategory'].'</td>';
echo '<td>'.$row['TipoStatus'].'</td>';
echo '<td>'.$row['TiposUrgencia'].'</td>';
echo '<td>'.$row['CustomerName'].'</td>';
echo '<td>'.$row['DayCreation'].'</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo $ticket_select;
$_SESSION['serviceID'] = $ticket_select;
'</table>';
?>
Is it a case issue?
$_SESSION['serviceID'] = $ticket_select;
<input type="submit" name="serviceID" value=<?php echo $row['ServiceID']?>
$ticket_select = $_POST['serviceID'];
Notice the middle one has a capital S on ServiceID and the other two are serviceID.

Categories