Passing Value of my Textbox named "title" to a query - php

I am currently new in PHP can you please guide me on how can I get the value of my textbox? and pass it to my sql query? where I want to become the value of my Column name Page? Please Guide me.
<?php
$sql_query="SELECT * FROM tblpost WHERE Page = '//what to put here?' ";
$result_set=mysql_query($sql_query);
while($row=mysql_fetch_row($result_set)){
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[6]; ?></td>
<td>
<a class="btn btn-primary" href="javascript:edt_id('<?php echo $row[0]; ?>')">Edit</a>
<a class="btn btn-danger" href="javascript:delete_id('<?php echo $row[0]; ?>')">Delete</a>
</td>
</tr>
<?php } ?>
this is my textbox.
<div class="form-group">
<label>Page Title</label>
<input name="title" type="text" value="<?php echo $fetched_row['Title']; ?>" required class="form-control" placeholder="Page Title">
</div>

You may want to search for PHP tutorials in addition to this answer.
First, you'll have to wrap your input field in some sort of form. The form will have to have a submit button that leads back to your page, using method GET.
Second, you can get parameters from a GET request in php with $_GET['name-of-input-field']. name-of-input-field is the value of the name attribute of the input field.
Then, you'll want to create a "PreparedStatement", into which you can bind this get parameter. This is a guide on PreparedStatements in PHP: http://php.net/manual/en/pdo.prepared-statements.php

Related

How to return values entered in a dynamic table textboxes if there is an error on form submit

Coud somebody please help me. I have a textbox name "txtremise" found in a dynamic table where user enter a discount %. I am trying to return the values entered in each textbox if there is an error during the form submit.
Lets say, there are 3 rows with 3 textboxes in the table. A user enter 5, 10, 15 respectively in each textbox. Let's say there was an error upon submitting the form, i need the values 5, 10, 15 back in their respective textboxes. Can somebody please assist?
Here is how i generate the textboxes:
<?php
while($iArticles < count($listeArticlePourUnDossier))
{
?>
<tr>
<td><?php echo ($listeArticlePourUnDossier[$iArticles]['ARTICLE_NOM']); ?></td>
<td>
<input type="text" name="txtremise[]" id="txtremise_<?php echo $iArticles; ?>" class="remise" value="<?php echo $_POST['txtremise'];" />
</td>
</tr>
<?php
$iArticles++;
}
?>
Thank you for helping.
<?php
$i=0;
while($iArticles < count($listeArticlePourUnDossier))
{
?>
<tr>
<td><?php echo ($listeArticlePourUnDossier[$iArticles]['ARTICLE_NOM']); ?></td>
<td>
<input type="text" value="<?php if(isset($_POST['txtremise'][$i])){ echo $_POST['txtremise_'][$iArticles];}?>" name="txtremise[]" id="txtremise_<?php echo $iArticles; ?>" class="remise" value="<?php echo $_POST['txtremise'];" />
</td>
</tr>
<?php
$i++;
$iArticles++;
}
?>
You still have that value in either $_POST or $_GET so check if the key is isset then echo the value. So all you need is to add value in your input

Searched Data Not Being Displayed

I am trying to put a search button on my website where, if the user search for the date all the data from the db that have that "certain date" will be displayed. But for some reason, the datas are not displaying. Can you guys help me figure this out?
Below are my codes for the search
<form action="file.php" method="post">
<input type="text" name="search" placeholder="YYYY-MM-DD">
<input type="submit" name="sub" value="Search Data">
</form>
<?php
if(isset($_GET['sub'])){
$search=$_GET['search'];
$search_q="SELECT * FROM table WHERE date='$search'";
$run_q=mysqli_query($con, $search_q);
while($rows=mysqli_fetch_array($run_q)){
$date_q=$rows['date'];
$impact_q=$rows['impact'];
$advice_q=$rows['advice'];
$typhoon_q=$rows['typhoon'];
$warning_q=$rows['warning'];
?>
<table>
<tr>
<td><?php echo $date_q; ?></td>
<td><?php echo $impact_q; ?></td>
<td><?php echo $advice_q; ?></td>
<td><?php echo $typhoon_q; ?></td>
<td><?php echo $warning_q; ?></td>
</tr>
</table>
<?php } } ?>
You used method type POST for your search form. But you are receiving form data using GET. So please receive the form data using $_POST. Replace $_GET['sub'] to $_POST['sub'] and $_GET['search'] to $_POST['search'].
Also please check your database table name. Did you give your table name: table?

Pass Two values when a Submit button is clicked -PHP

This table is displaying details about my workload table per row. And when I click my "pre-View_attend" submit button I want to get the "WorkloadID" and "subjectID" of that workload row where the submit button was clicked.
Currently I have a hidden field containing the subject ID which is "subjectID_hidden", and when I try to get the "WorkloadID" and "subjectID" by using isset.
It seems that I can't accurately get the exact subject ID of the row where I've clicked the "pre-View_attend" submit button.
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["subjectName"]; ?></td>
<td><?php echo $row["className"]; ?></td>
<td><?php echo $row["RoomNumber"]; ?></td>
<td><?php echo $Sched_Days.'<br>'.$FST.' To '.$FET;?></td>
<td><button style="font-size:15px;" type="button" id="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary view_StudentList">View Student List</button></td>
<td><button style="font-size:15px;" type="submit" name="gettingAttendance" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">Take Attendance</button></td>
<td><button style="font-size:15px;" type="submit" name="pre-View_attend" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">View Attendance</button></td>
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
</tr>
<?php } mysqli_close($connect);?>
</tbody>
/*
I'm getting the workload ID accurately, but the subject ID is incorrect.
I believe I'm getting the last subject ID that my query produced
*/
<?php
if(isset($_POST['pre-View_attend']))
{ $FWID=$_POST['pre-View_attend'];
$FSJID=$_POST['subjectID_hidden'];
echo"Workload ID: $FWID SubjectID: $FSJID";
}
?>
You get the last value because you have a bunch of hidden fields with the same name.
You could name them subjectID_hidden[<?=$row['WorkloadID'];?>] to get an array to then get the subjectID by WorkloadID:
$subjectID = $_POST["subjectID_hidden"][$_POST["pre-View_attend"]];
You need to query the table for something unique to the row you want to retrieve information from. This unqiue data should already be existing on the page, encoded into the button/form.
Example;
<?php
if(isset($_POST['pre-View_attend'])) {
global $db;
$select = "select * from mytable where unique='$unique'";
$connect = mysqli_query($db, $select);
while($row=mysqli_fetch_array($connect)) {
$myfirstVariable = $row['myfirstVariable'];
$mysecondVariable = $row['mysecondVariable'];
$show = "$myfirstVariable $mysecondVariable";
}
}
?>
Then you can call $show when you want in the HTML.
p.s. there are quite a few errors in your html like;
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
should be
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>" />
self closing at the end with forwards slash.

Update a value in a table using checkbox in PHP

I'm a beginner in php and I have a problem regarding checkbox.
First thing is that, in the form I don't know if name=medID[] is specifically used as an array. or a normal string like medID will work? and how exactly is it useful to use an array.?
When I'm updating the value in query both $quan and $medID values are not passing in the query.
In browser it shows "Alloted Succesfully" but the database value of quantity is not changing. when i replace $quan and $imp value to some integers then it works fine.
<tbody>
<tr>
<form method="post" action="ytube.php?array=hospitalstock&hospitalID=<?php echo $opened['hospitalID']; ?>&id=allot" role="form">
<div class="form-group">
<td class="vcenter"><input type="checkbox" name="medID[]" id="check" value="<?php echo $list['medID']; ?>" /></td>
</div>
<td><?php echo $list['item'] ?> </td>
<td><?php echo $list['price'] ?> </td>
<td><?php echo $list['quantity'] ?> </td>
<td><?php echo $list['subtotal'] ?> </td>
<div class="form-group">
<td><input type="text" name="quantity" id="quantity" class="form-control" /> </td>
</div>
</tr>
<?php }} ?>
<div class="form-group">
<input class="submit" type="submit" value="Allot Medicine" name="submit" class="form-control" />
</div>
</form>
</tbody>
</table>
<?php
$id=$_POST['medID'];
$quan=$_POST['quantity'];
if(isset($_POST['submit'])){
if(empty($id) || $id==0){
echo 'Select medicines to allot ';
}else{
echo $quan;
$imp= implode(", ",$id);
$q="UPDATE hospitalstock SET quantity= (quantity - '.$quan.') WHERE medID IN('.$imp.')" ;
$r=mysqli_query($conn, $q);
if(isset($r)){
echo 'Alloted Succesfully';
}
}
}
?>
Yes a normal string like madID will work just aswell.
name="medID" => $_post['medID']
name="medID[]" => $_post['medID'][0] de last [0] will get you the first element of the array
An array could be really helpful when it's a dynamically created form. A form where the number of inputs is not set, for example a contact form where you can click on a plus icon to add another text input for multiple phone numbers. Bacause its unknown how many phone numbers someone have its easier to just retrieve one variable as an array and iterate over this array after.
Don't you get an error like:
Notice: Undefined index: medID in ....
In your code you have name="medID[]" and $_post['medID']. So your form is sending an array but you retrive a normal variable. Just delete [] from name="medID[]"
Because of that if(empty($id) $id will always be empty so you don't even reach your query.
Few things to say; first I don't understand the concept of using a query string for action when you are using post as a method for submitting the values. Secondly if you are trying to consume the values from the query string as well; then I can't find the $_GET[] in your entire program. Third is a suggestion to use $_REQUEST[] when you are not sure about the get or post collections. Also, the name="medID[]" won't create any array for PHP. Is the ytube.php the same page where you have created this form?

How to connect dynamic db value from one html element to another?

I had a hard time to formulate this question so it would be understandable, but here goes.
I have a page called apartments.php. On this page I list a number of rows from my database on apartments that you can show interest to rent.
The apartments are printed out like this:
<?php
foreach ($apartments as $apartment) { ?>
<tr>
<td><?php echo $apartment['apartments_room'] ?></td>
<td><?php echo $apartment['apartments_area'] ?></td>
<td><?php echo $apartment['apartments_rent'] ?></td>
<td><?php echo $apartment['apartments_address'] ?></td>
<td><?php echo $apartment['apartments_floor'] ?></td>
<td><?php echo $apartment['apartments_city'] ?></td>
<td><?php echo $apartment['apartments_freeFromDate'] ?></td>
<td>Apply!</td>
</tr>
<?php } ?>
Each apartment that gets printed out has its own button. When you press this button, a modal / popup will show.
And here's my problem.
When the modal pops up I want it to show the information of the apartment from witch I pressed the button.
So the data-reveal-id value from the button (that represent the specific apartment's db-id) is what I need to get into the modal divs id-value. Additionally I need to write out the address of that apartment (the db column 'apartments_address').
So this is how I need it to be:
<div id="HERE I NEED TO GET THAT ID" class="reveal-modal">
<h2>AND HERE I NEED TO PRINT OUT THE NAME OF THE ADDRESS</h2>
// Here i make room for an application form, but that's irrelevant...
</div>
This shouldn't be a hard thing, right? I hope you can help me out. Thanks!
(For more details if it matters, I'm using the modal of Foundation Zurb 4.)
First, your html isn't linking to a new page:
<a href="#" id="<?php echo $apartment['apartments_id'] ?>">
This should probably look something like this:
<a href="streetEdit.php?id=<?php echo $apartment['apartments_id'] ?>">
Then you can create a new php page named streetEdit.php
in that page, you can select the appropriate row from the database
$sql = "select * from myApartmentsTable where id = {$_GET['id']}";
See how the ID passed in the URL above shows up in $_GET?
next, do your query using the sql and output it on the page.
inside that you need an html form. Use a hidden input to keep track of the ID you are editing:
<form method="POST" action="updateStreet.php">
<input type="hidden" name="id" value="<?php echo $apartment['apartments_id'] ?>">
<input type="text" name="streetName" value="<?php echo $apartment['streetName'] ?>"
<input type="submit">
</form>
Then you need to write updateStreet.php. Since the form was POSTed you will use $_POST instead of $_GET
$id = $_POST['id'];
$streetName = $_POST['streetName'];
Note, you should use PDO for doing your sql queries
$sql = "update apartments set streetName = :name where id = :id";
$pdo = new PDO();
$stmt = $pdo->prepareStatement($sql);
$stmt->bindParam(':name', $streetName );
$stmt->bindParam(':id', $id);
http://php.net/manual/en/pdo.prepared-statements.php
This will prevent SQL injection attacks.. Ask further if you have more questions

Categories