I'm having a problem to upload images using a form. This is my form code :
<?php
echo '<table width="70%">';
echo '<tr>';
echo '<td>Add Promotion</td>';
echo '</tr>';
echo "<form action='addpromotion_post.php' method='post' enctype='multipart/form-data' name='form1' id='form1'>";
echo '<tr>';
echo "<td> </td>";
echo '</tr>';
echo '<tr>';
echo "<td>Promotion :</td>";
echo "<td><textarea name='promotion1' rows='2'></textarea></td>";
echo '</tr>';
echo '<tr>';
echo "<td>Valid From :</td>";
echo "<td><input type='text' size='40' name='validfrom1' value='promotion' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic1' id='pic1' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic2' id='pic2' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic3' id='pic3' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td> </td>";
echo '</tr>';
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>';
This is my post code :
<?php
//session_start();
include_once("connection.php");
$promotion= $_POST["promotion1"];
$validfrom= $_POST["validfrom1"];
$promotion= mysql_real_escape_string($promotion);
$validfrom= mysql_real_escape_string($validfrom);
$sql = "insert into promotion(proid, promotion, validfrom) values ('', '$promotion', '$validfrom')";
mysql_query($sql) or die ("Error in query: $sql");
$lastid=mysql_insert_id();
$file = "slider_imagesClient/".$lastid."/";
mkdir($file);
$pic1 = $_FILES['pic1']['name'];
$pic2 = $_FILES['pic2']['name'];
$pic3 = $_FILES['pic3']['name'];
print_r($pic1);
$ext1 = pathinfo($pic1,PATHINFO_EXTENSION);
$ext2 = pathinfo($pic2,PATHINFO_EXTENSION);
$ext3 = pathinfo($pic3,PATHINFO_EXTENSION);
//$fileEnote = $_FILES['eNote']['name'];
$path1 = $file.$pic1.'.'.$ext1;
$path2 = $file.$pic2.'.'.$ext2;
$path3 = $file.$pic3.'.'.$ext3;
print_r($path1);
$ory1 = $sfish.$pic1.'.'.$ext1;
$ory2 = $sfish.$pic2.'.'.$ext2;
$ory3 = $sfish.$pic3.'.'.$ext3;
if (!file_exists($path1))
{
move_uploaded_file ($_FILES['pic1']['tmp_name'], $path1) or die ("Error");
}
if (!file_exists($path2))
{
move_uploaded_file ($_FILES['pic2']['tmp_name'], $path2) or die ("Error");
}
if (!file_exists($path3))
{
move_uploaded_file ($_FILES['pic3']['tmp_name'], $path3) or die ("Error");
}
if(!empty($pic1)) {
$sql5= "INSERT INTO pimage (id, proid, image)
VALUES ('', $lastid, '$ory1')";
mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}
if(!empty($pic2)) {
$sql5= "INSERT INTO image (resID, image)
VALUES ($lastid, '$ory2')";
mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}
if(!empty($pic3)) {
$sql5= "INSERT INTO image (resID, image)
VALUES ($lastid, '$ory3')";
mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}
?>
print_r($pic1) are not outputting anything. print_r($path1) just outputting slider_imagesClient/50/.
How can I solve this? I'm suspecting error in the form but I couldn't find any. Thank you very much :D. Really appreciate your help.
echo '<table width="70%">';
echo '<tr>';
echo '<td>Add Promotion</td>';
echo '</tr>';
echo "<form action='addpromotion_post.php' method='post'
enctype='multipart/form-data' name='form1' id='form1'>";
change like this
echo "<form action='addpromotion_post.php' method='post' enctype='multipart/form-data' name='form1' id='form1'>";
echo '<table width="70%">';
echo '<tr>';
echo '<td>Add Promotion</td>';
echo '</tr>';
Related
enter image description hereI am new to PHP and Mysql. i am trying to create a quiz project where multiple questions in a quiz will be inserted into the database once. How can i go about it. I have searched for solutions here and i tried using foreach. i am not having errors but it is not uploading
here is my form
<form class="quiz-questions">
<h1>Title Of Quiz</h1>
<?php
for($counter = 1; $counter <= $_SESSION['quiz_no']; $counter++)
{
echo "<div class='quiz-left'>";
echo "<div>";
echo "<label for='textarea' class='sn-quiz'>$counter</label><textarea name='question[]' class='textarea' id='example-three' placeholder='Enter the Question' cols='60' rows='3' ></textarea>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionA'>";
echo "<input type='text' name='optionA[]' placeholder='optionA'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionB'>";
echo "<input type='text' name='optionB[]' placeholder='optionB'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionC'>";
echo "<input type='text' name='optionC[]' placeholder='optionC'>";
echo "</div>";
echo "<div class='option-tag'>";
echo "<input type='checkbox' name='op1[]' value='optionD'>";
echo "<input type='text' name='optionD[]' placeholder='optionD'>";
echo "</div>";
echo "</div>";
}
?>
<input type="submit" name="insert_quiz" class="btn-primary btn btn-lg" placeholder="SUBMIT">
</form>
and here is my submit code
<?php
if(isset($_POST['insert_quiz'])) {
$quiz_add_id = $_SESSION['quiz_id'];
$count = $_SESSION['quiz_no'];
$i = 0;
foreach ($_POST as $val) {
$question = $_POST['question'][$i];
$optionA = $_POST['optionA'][$i];
$optionB = $_POST['optionB'][$i];
$optionC = $_POST['optionC'][$i];
$optionD = $_POST['optionD'][$i];
$op1 = $_POST['op1'][$i];
$query = "INSERT INTO quizzes (quiz_add_id, quiz_question, quiz_option_A, quiz_option_B, quiz_option_C, quiz_option_D, quiz_option_correct)";
$query .= "VALUES ('{$quiz_add_id}', '{$question}', '{$optionA}','{$optionB}','{$optionC}','{$optionD}', '{$op1}')";
$create_quiz_query = mysqli_query($connection,$query);
$i++;
}
if(!$create_quiz_query ){
die('QUERY FAILED' . mysqli_error($connection));
}
}
?>
Got a table which stores information about pupils.
CREATE TABLE pupis (name TEXT, city TEXT, caretaker TEXT);
By opening this web-site, info from this table is showed.
There is a "Sort" button, which sorts all the pupils by their cities.
Also by clicking this button, a drop down list occurs near every pupil.
This list allows to choose a caretaker for a pupil.
This way, my question is how to send all chosen caretakers from numerous drop down lists by clicking single button? (UPDATE TABLE)
I am sorry I am new to PHP and right now I do not understand how to be here. I can only suggest to create arrays dynamically that consists of pupil id and its caretaker.
I've created this web-site especially for this question and this code is how that site works:
<?php
mb_internal_encoding("UTF-8");
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die('' . mysql_error());
mysql_select_db($mysql_dbname) or die('');
mysql_set_charset('utf8');
$query = 'SELECT * FROM pupils';
$result = mysql_query($query) or die(mysql_error());
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);
mysql_close($link);
?>
<form action="" method="post">
<input type="submit" name="sort" value="Sort">
</form>
<?php
if (isset($_POST["sort"])) {
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die(mysql_error());
mysql_select_db($mysql_dbname) or die();
mysql_set_charset('utf8');
$query = 'SELECT * FROM pupils WHERE city = "Moscow";' or die("died");
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo "<h1>Moscow</h1>";
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\t\t";
}
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker\" required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
echo "</tr>\n";
}
echo "</table>\n";
}
$query = 'SELECT * FROM pupils WHERE city = "London";' or die("died");
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo "<h1>London</h1>";
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\t\t";
}
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker\" required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
echo "</tr>\n";
}
echo "</table>\n";
}
mysql_free_result($result);
mysql_close($link);
}?>
<form action="" method="post">
<input type="submit" name="send_to_caretakers" value="Send">
</form>
<?if (isset($_POST["send_to_caretakers"])) {
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die(' ' . mysql_error());
mysql_select_db($mysql_dbname) or die('');
mysql_set_charset('utf8');
echo "Sent to the caretakers";
$temp = $_REQUEST['choose_caretaker'];
echo $temp;
// Have no idea how it can designed
mysql_close($link); // Закрываем соединение
}
?>
To use array for "select name" e.g choose_caretaker[pupil_id]
add in "multiple" in select box to select mutiple
render your result in php. you will see your caretaker under a pupil in array format.
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker[$link['id']]\" multiple required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
i would like to know how can i make a multiple search criteria with 2 or more textboxes and only one submit button.
my script is:
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE nume= '{$search_term}' ";
}
$query = mysql_query($sql) or die (mysql_error());
echo "<form name ='search_form' method='POST' action='search.php'>";
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box' />";
echo "<input type='submit' name='search' value='Cauta' /></center>";
echo "</form>";
and my results page that shows after search page:
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE nume= '{$search_term}'";
}
echo "<center>\n";
echo "<table border='1'>";
echo "<thead>";
echo "<tr><th>Id</th>";
echo "<th>Nume</th>";
echo "<th>Localitate</th>";
echo "<th>Judet</th>";
echo "<th>Sector Financiar</th>";
echo "<th>Link</th></tr>";
echo "</thead>";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td>"; echo $a_row['id']; echo "</td>";
echo "<td>"; echo $a_row['nume']; echo "</td>";
echo "<td>"; echo $a_row['localitate']; echo "</td>";
echo "<td>"; echo $a_row['judet']; echo "</td>";
echo "<td>"; echo $a_row['sector_financiar']; echo "</td>";
echo "<td>"; echo "<a href='results.php?id={$a_row['id']}'>{$a_row['link']}</a>" ; echo "</td>";echo "</tr>";
echo "</table>";
$sql = "select * from wp_studenti ";
if (isset($_POST['search'])) {
$search_term_by_Cauta = mysql_real_escape_string($_POST['search_box_1']);
$search_term_by_localitate = mysql_real_escape_string($_POST['search_box_2']);
//If you want both search mandatory, use "AND" Operator otherwise use "OR". If you want approximate search use "LIKE" Operator in bellow SQL
$sql .= " WHERE nume= '{$search_term_by_Cauta }' OR localitate = '{$search_term_by_localitate }' ";
}
$query = mysql_query($sql) or die (mysql_error());
echo "<form name ='search_form' method='POST' action='search.php'>";
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box_1' />";
echo "<h3>localitate:</h3> <input type='text' name='search_box_2' />";
echo "<input type='submit' name='search' value='Cauta' /></center>";
echo "</form>";
Well you need another search box:
echo "<center><h3>Cauta:</h3> <input type='text' name='search_box1' /><input type='text' name='search_box2' />";
And you need to use that value in your SQL:
if (isset($_POST['search'])) {
$search_term1 = mysql_real_escape_string($_POST['search_box1']);
$search_term2 = mysql_real_escape_string($_POST['search_box2']);
$sql .= " WHERE nume= '{$search_term1}' OR nume= '{$search_term2}'";
}
But you will have to do some thinking about how the search should work, is it supposed to match exactly one OR the other? If you want the text to contain instead of exactly match, you can use the syntax nume LIKE '%searchword%'
Use mysqli instead of mysql, which is depreciated. By PHP, something like this;
<form method='post'>
<input type='hidden' name='srch' val='1'>
Search Type1: <input type='text' name='s1'>
<br>
Search Type2: <input type='text' name='s2'>
<button>Submit</button>
</form>
<?php
if(isset($_POST['srch']))
{
if(!empty($_POST['s1']))$search = $_POST['s1'];
else if(!empty($_POST['s2']))$search = $_POST['s2'];
else die ('No criteria entered');
rest of your code...
}
?>
Also see functions like mysqli_real_escape for security reasons.
Please I'm a newbie in php and mysql I want you to help me on how to check if value to be inserted into a database is greater than or lesser than, if greater than it should insert pass, thanks.
this the code
$query="select * from ats_question";
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or die(mysql_error());
if(!isset($_SESSION[qn]))
{
$_SESSION[qn]=0;
mysql_query("delete from ats_useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
$_SESSION[trueans]=0;
}
else
{
if($submit1=='Next Question' && isset($ans))
{
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,
true_ans,your_ans)values('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
if($ans==$row[7])
{
$_SESSION[trueans]=$_SESSION[trueans]+1;
}
$_SESSION[qn]=$_SESSION[qn]+1;
}
else if($submit=='Get Result' && isset($ans))
{
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,
your_ans) values
('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
if($ans==$row[7])
{
$_SESSION[trueans]=$_SESSION[trueans]+1;
}
echo "<h1 class=head1> Result</h1>";
$_SESSION[qn]=$_SESSION[qn]+1;
echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
$w=$_SESSION[qn]-$_SESSION[trueans];
echo "<tr class=fans><td>Wrong Answer<td> ". $w;
echo "</table>";
mysql_query("insert into ats_result(login,test_id,grade,score) values
('$login',$tid,grade,$_SESSION[trueans])")
or die(mysql_error());
echo "<h1 align=center><a href=ats_review.php> Review Question</a> </h1>";
unset($_SESSION[qn]);
unset($_SESSION[sid]);
unset($_SESSION[tid]);
unset($_SESSION[trueans]);
exit;
}
}
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error Occured</h1>";
session_destroy();
echo "Please <a href=index.php> Start Again</a>";
exit;
}
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
echo "<div align=\"center\" class=\"style2\"><strong> Ats Examination </strong></div>";
echo "<div id=\"\"></div>";
echo"<p id=\"note\"></p>";
echo "<form name=myfm id=myfm1 method=post action=ats_exam.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ". $n .": $row[2]</style>";
echo "<tr><td class=style6><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style6> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style6><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style6><input type=radio name=ans value=4>$row[6]";
if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit1 value='Next Question'> </form>";
echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
echo "</table></table>";
?>
i want to check if the trueans is greater than 15 it should insert pass if not fail in the grade value
mysql_query("insert into ats_result(login,test_id,grade,score) values
('$login',$tid,grade,$_SESSION[trueans])")
Thanks I have finally get solution to my question this what my code now look like mysql_query("insert into ats_result(login,test_id,score,grade)values('$login','$tid',$_SESSION[trueans],IF($_SESSION[trueans]>=10,'Pass','Fail')"); thanks all for the response and challenges throw to me.
<?php
$status=&$_POST['status'];
$from=&$_POST['date_from'];
$to=&$_POST['date_to'];
$conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error());
if(isset($_POST['sub'])){
$ins="insert into driver_status(driver_name,status,date_from,date_to)
select Driver_name,'$status','$from','$to' from driver_master";
if(mysqli_query($conn,$ins)){
echo "added";
}
else{
echo "NOT".mysqli_error($conn);
}
}
$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['Driver_name']."</td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
?>
<td>
<select name="status">
<?php
while($row1=mysqli_fetch_assoc($query1)){
$st=$row1['d_status'];
echo "<option value='$st'>$st</option>";
}
?>
</select>
</td>
<?php
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
That is my code. I want to save option selected from 4 drop down list to mysql. When I submit the form, the value selected from last box are getting saved in all rows of mysql table. Now, please tell me what should I do?
I am getting drop down box values from database table properly so what is the problem?
this is your code.
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from = $_POST['date_from'];
$to = $_POST['date_to'];
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
if (mysqli_query($conn, $ins)) {
echo "added";
} else {
echo "NOT" . mysqli_error($conn);
}
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td></tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
this is similar to other textbox
$op="select * from client where active=0";
$op1=mysql_query($op);
echo '<select name="c" id="c" style="width:160px;" required>';
while ($row = mysql_fetch_array($op1))
{
echo $s=$row["c_name"];
if($company!=$s)
{
echo '<option value="'.$row["c_name"].'">'.$row["c_name"].'</option>';
}
}
$c=$_REQUEST['c'];
$sql="insert into project c_name) values('$c')";
$sql1=mysql_query($sql);