I have a form that was generated with php
Below is in my account.php
$student_id is set by a session and I am finding all the file_names that are associated with that student_id. The I generated an HTML form using php where each of the file_names for that student are displayed in a checkbox format with a description input below each checkbox.
<?php
$find = mysql_query("SELECT file_name FROM chemlab_files WHERE student_id =
'$student_id'") or die ("Could not search!");
while($row = mysql_fetch_array($find)){
$file_name = $row['file_name'];
$_SESSION['file_name'] = $file_name;
echo "<label class='checkbox'><input type='checkbox' name='file_name[]'
value='{$file_name}'>$file_name</label><br><input type='text' name='description[]'
value='' placeholder='description'>";
}
?>
This is now part of the handler.php I am having trouble with it does insert but it insert blanks for the checkboxes not checked off
if(is_array($_POST['description'])){
foreach(str_replace('#', '', $_POST['description']) as $strip){
$description_backslash = $strip . '/';
$description = mysql_real_escape_string($strip);
$query = "INSERT INTO file_list(description) VALUE ('$description')";
$q= mysql_query($query) or die ('Error posting data');
//print_r($description);
//print '<br>';
}
Checkboxes that are not checked, do not get sent to the server so if not all checkboxes are checked, there will be a mis-match between the checkbox indices and the file_name inputs.
To avoid that problem, you would have to add indices to your form fields so that you know what description belongs to what file_name and to be able to loop over the description's and discard the ones that are not set.
Related
I want to enroll the student and insert the student id into Mysql database after I check and submit the checkbox value, but I already tried so many ways but still cannot...
This is the php code
<?php
if (isset($_POST['submitxd'])) {
foreach ($_POST['enrol'] as $items) {
$insert = $link->query("INSERT INTO student_course(studentID) values ('$items')");}
}
?>
This is the html code
$result = $link->query("SELECT * FROM student WHERE programmeName = '$programme' AND intake = '$intake'");
while ($row = mysqli_fetch_array($result)) {
echo "<tr>
<td>".$row['studentID']."</td>
<td>".$row['studentName']."</td>
<td>".$row['studentGender']."</td>
<td>".$row['studentContact']."</td>
<td>
<input type='checkbox' name='enrol[]' value='".$row['studentID']."'>
</td>
</tr>";
}
check whether your array contains values or not:
echo "<pre>";
print_r($_POST['enrol']);
echo "</pre>";
if not, you should write html code properly i.e. check form tag and its action path carefully and before submitting the form, remember to check out the checkbox
I'm trying to pass a value which is input into a box on one Php page (itinerary.php) into another Php page ('submit.php') so it can, from there, be saved into a database. But I can't quite figure out how to get it across. I've tried using GET as you can see from code below, but I am already using a GET statement to receive and acknowledge another value from that very same page 'submit'. I guess I am overcomplicating it, but my knowledge of Php is still pretty limited at this stage so any ideas would be appreciated!
This is an extract from the itinerary.php file (it sits within a Bootstrap/Html framework. Note the entry which contains the input box for the sequence number).
<h3><br>YOUR ITINERARY</h3>
<?php
//Display contents of itinerary
if(!empty($_SESSION['itinerary'])){
//Retrieve details of each location in array from database
$query = "SELECT * FROM locations WHERE loc_id IN (";
foreach ($_SESSION['itinerary'] as $loc_id=>$value)
{$query.=$loc_id.',';}
$query = substr($query, 0, -1).')ORDER BY loc_id ASC';
$result = mysqli_query($db, $query);
echo'<table><tr><th colspan="5">LOCATIONS IN YOUR ITINERARY</th></tr>';
//Display locations in array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$loc_id = $row['loc_id'];
echo"<br><td>{$row['loc_name']}</td></tr><br>
<td>{$row['loc_desc']}</td>
<td><p>Seq. No</p><input type=\"text\" size=\"3\" name=\"sequence\" value=????????>NOT SURE WHAT TO ADD INTO THIS LINE TO RETAIN THE INFO THAT IS INPUT</td>
<td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
</tr><br></table>";
}//while
print_r($_SESSION);
mysqli_close($db);
}//if
else {echo '<p><br>Your itinerary is empty.<br></p>';}
echo '<br><p>
<a href=submit.php?submit>Save itinerary</a>
<a href=clear_itin.php?clear>Clear itinerary</a>
Your details
Logout
</p>';
?>
And this is where I am trying to receive it and then use it in a SQL command to add to the database. (Again, this is within a Bootstrap framework)You can ignore the first SQL Insert statement as it is passing other info in successfully anyway..
<div class="row">
<div class="col-md-9">
<?php
if (isset($_GET['sequence'])){
$sequence = $_GET['sequence'];
}//if
if (isset($_GET['submit'])){
$query = "INSERT INTO itineraries (user_id, date_created) VALUES (".$_SESSION['user_id'].", NOW())";
$result = mysqli_query($db, $query);
$itinerary_id = mysqli_insert_id($db);
$retrieve_locs = "SELECT * FROM locations WHERE loc_id IN (";
foreach ($_SESSION['itinerary'] as $id=>$value)
{$retrieve_locs.=$id.',';}
$retrieve_locs = substr($retrieve_locs, 0, -1).')ORDER BY loc_id ASC';
$result = mysqli_query($db, $retrieve_locs);
//Store items in itin_locs db
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)){
//This is the command I have been trying to use, commented out. The second one below this works fine, but obv doesn't input a sequence number.
//$insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id, sequenceNo) VALUES ($itinerary_id, ".$row['loc_id'].", $sequence)";
$insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id) VALUES ($itinerary_id, ".$row['loc_id'].")";
$insert_result = mysqli_query($db, $insert_locs);
echo mysqli_error($db);
if ($insert_result === FALSE) {
die("Query failed!" . mysql_error() . $insert_locs);}
}//while
mysqli_close($db);
echo"<p>Your itinerary is saved!. Itinerary number is #".$itinerary_id."</p><br>";
$_SESSION['itinerary']= NULL;
echo '<p>
Your details
Logout
</p>';
}//if
else {echo '<p>Your itinerary is empty.<br></br></p>';}
?>
Use a form on your HTML page to hold all the input fields in the table.
Also instead of anchor "a" tag use a submit button to submit the form to other page.
Use some thing like:
Send value of submit button when form gets posted
hii am amateur in php and making a website in which i am uploading an image,storing it in database(with an autoincrement variable) and displaying it on page in descending order so it may seem as latest at top and latter below it,,i need to add a textbox for comment and username(cuz i haven't made login page as m not gonna host it somewhere)
i knwo how to add it for upload image's section as only 1 image is gonna get uploaded
problem is how to display it, i have used
while ($image = mysqli_fetch_assoc($sql))
{
echo image code....
}
Now i in the div to display image i'll incorporate 2 textboxes and evertime after while loop ill display values from dB accordingly but when i want to edit the textbox after uploading more than 1 image,how to know which textbox is being edited as every textbox would have same textbox name
should i compare the image contents ?as the image iteself is stored in binary format? but that wud be a slow process i guess
plz suggest som idea or method to do so basically its like instagram(but very basic)....
First create images table:
images table
image_id | url
Then creare seperate table for images comments:
images_comments table
comment_id | image_id | user | text | date
Where:
comment_id: integer , Auto Increment
image_id: integer referring to id in images table
user: should be user id referring to separate users table, but because you do not have login system you can store user name here as varchar.
text: varchar
date: datetime
To show images + comments:
$sql = "SELECT * FROM images";
while ($image = mysqli_fetch_assoc($sql))
{
$imageUrl = $image["url"];
$imageID = $image["image_id"];
//Show image:
echo "<img src='$imageUrl ' />";
//Get comments:
$sql2 = "SELECT * FROM images_comments WHERE image_id=$imageID";
while ($comment = mysqli_fetch_assoc($sql2))
{
$text = $comment["text"];
$userName = $comment["user"];
$date = $comment["date"];
//show username and comment date
echo $userName . " " .$date . "<br>";
//Show comment text:
echo $text;
}
}
To add a comment to images
if you want a comment form under each image then in the While loop we used before add the following code:
while ($image = mysqli_fetch_assoc($sql))
{
.......... previous code goes here .........
//in the form we store imageID in hidden input field
//so that we can know to which image the form belongs
echo "<form method='post' action='page.php'>
<input type='text' name='username'/>
<textarea name='commentText'></textarea>
<input type='hidden' name='imageID' value='$imageID'/>
<input type='submit' name='submitComment' value='Submit'>
</form>";
}
then in page.php
if(isset($_POST["submitComment"]))
{
$username = $_POST["username"];
$text = $_POST["commentText"];
$imageID = $_POST["imageID"];
$date = date("Y-m-d H:i:s");
$sql = "INSERT INTO images_comments (image_id,user,text,date) VALUES
($imageID,$username,$text,$date)";
mysqli_query($con,$sql);
}
I am trying to insert into a database posted form values, retrieved from an html form that was generated via php. However it is not inserting the values, however I do see the values when I print the posted variables. Here is the code.
This is the form written in php that outputs html
<?php
$find = mysql_query("SELECT file_name FROM chemlab_files WHERE student_id = '$student_id'") or die ("Could not search!");
while($row = mysql_fetch_array($find)){
$file_name = $row['file_name'];
$_SESSION['file_name'] = $file_name;
echo "<label class='checkbox'><input type='checkbox' name='file_name[]' value='{$file_name}'>$file_name</label><br><input type='text' name='description[]' value='' placeholder='description'>";
}
?>
This is the form handler
//description
if(isset($_POST['description'])){
foreach($_POST['description'] as $description){
$file_name_description = mysql_real_escape_string($description);
//print_r($file_name_description);
}
}
$result = mysql_query("SELECT * FROM chemlab_files WHERE student_id='$student_id'") or die (mysql_error());
//if the student_id does exist, proceed
if($result==1){
//insert into chemlab_files update description
$insert_file_name_description = mysql_query("UPDATE chemlab_files SET description='$file_name_description' WHERE file_name='$file_name'");
}else{
echo 'something went wrong during sharing!';
exit();
I can print the variable $file_name_description but cannot insert into the database.
To be more clear the form has multiple fields, for ever file_name (checkbox with value ="$file_name") has a description field. When I do print_r for $file_name and $description I get the values for all the fields. However when I go to insert the description into the database nothing happens.
INSERT INTO table_name
VALUES (value1, value2, value3,...)
you need this.
If you are trying to create a new database record, shouldn't your sql be an INSERT statement like
INSTERT INTO chemlab_files SET (description, name) VALUES ('$file_name_description','$file_name')
I see two problems:
I do not understand why the text input in your HTML is an array (description[])
I don't see why you iterate it at the beginning, and you always set the $file_name_description to the last value. Are you trying to append all posted description inputs to the $file_name_description variable? If yes, then you should write .= instead of =
I need help for this problem that I've been trying to solve for a while (I'm new in PHP). I have a form with several checkboxes whose values are pulled from a specific table of the database. I managed to display them in the form, but cannot insert their values into the table connected to that specific page since there is ony one column.I want to enter all the selected values of the checkbox into that single column separated by commas.
Here's the code:
url <BR><?php $query = "SELECT url FROM webmeasurements";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{ $url = $row[0];
echo "<input type=\"checkbox\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
$checkbox_values = implode(';', $_POST['url']); }
?>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['url']))
{ echo $url;
foreach ($_POST['url'] as $statename)
{
$url=implode(',',$statename)
}
}
$q="insert into table (url) values ($url)";
?>
You should give each checkbox a different name, not name="url" for all, and then check if(isset($_POST['checkbox_name'])).