Sending Email From PHP Script - php

I have a page which is used for booking room for meetings. I have form on which all the details for booking is required. I have fetched email id from the MySql db.I want to send email on click of submit button. I have 2 files namely home.php & insert.php. In home.php i have the form & insert.php inserts the data into the db. I tried the email functionality code but it's not working.
Please Help
room.php
<?php session_start();
if(isset($_GET['selecteddate']))
{
$selecteddate=$_GET['selecteddate'];
}
else
{
$selecteddate=date("Y-m-d");
}
?>
<html>
<head><title>MRA</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<style>
#heading{ text-align:right;font-size: 20px;float:right;margin:50px 20px;}
a:hover{ color:#BEBEBC; }
a{ color:black;}
#img{ margin:10px 10px; }
#td{border-radius: 5px;border-style: solid;}
.name-error,.meeting-error { color: red; margin: 0 10px; }
#plan { position: fixed; left: 50%; margin-left: -239px; top: 50%; margin-top: -150px; z-index: 10;background-color: #fff; padding: 10px; }
#plan-bg { width: 100%; height: 100%; position: fixed; background: #000; opacity: 0.8; top: 0;display: none; z-index: 5; }
.close { position: absolute; top: 5px; right: 10px; cursor: pointer; }
.close:hover { text-decoration: underline; }
#datepicker { width: 40%; float: left; }
.calender-date { padding: 10px 10px; }
#time-slot-msg,#alert-msg { position: fixed; top: 50%; left: 50%; background: #fff; font-size: 25px; padding: 10px 20px; margin: -50px 0 0 -184.5px; display: none; z-index: 10;border-radius: 5px; }
#time-slot-msg p,#alert-msg p { font-weight: bold; }
#time-slot-msg .close,#alert-msg .close { font-size: 15px; right: 20px;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
var $index;
var systemdate = new Date();
$('.meeting-error').hide();
$(".meeting-timetable tr > td + td").click(function(e) {
if($(this).hasClass('active')) {
e.preventDefault();
alert('The selected time slot has already been assigned');
} else {
var $thisVal = $(this).prevAll('td:first-child').text();
var hour=$thisVal.substr(0,2);
var min=$thisVal.substr(3,3);
var d = new Date ("<?php print $selecteddate; ?>");
var n = d.getFullYear();
var m = d.getDate();
var k = d.getMonth();
var userdate=new Date(n,k,m,hour,min,0);
if( systemdate > userdate )
{
alert("Select Time Slot greater than the Current Time");
return;
}
$index = $(this).index();
// Start time.
$(".start-time option").each(function() {
if (($(this).text() === $thisVal)) {
$(this).attr('selected', 'selected');
$(this).prevAll().attr('disabled', 'disabled');
}
});
// Change end time.
$(".end-time option").each(function() {
if (($(this).text() === $thisVal)) {
$(this).next().attr('selected', 'selected');
$(this).attr('disabled', 'disabled');
$(this).prevAll().attr('disabled', 'disabled');
}
});
// Date input select.
var $date = $('.meeting-timetable tr:first-child th').text();
var dateFormat = $.datepicker.formatDate('yy-mm-dd', new Date($date));
$('.date').val(dateFormat);
$('#meeting').val('');
$('.meeting-error').hide();
// Show dialog box.
var _cellIndex = $(this)[0].cellIndex;
var _rowIndex = $(this).closest('tr')[0].sectionRowIndex;
var total_row = $('.meeting-timetable tr').length - 1;
if( _rowIndex == total_row && _cellIndex == 1 ) {
alert('You cannot select this time slot');
}
else $('#plan, #plan-bg').fadeIn();
}
});
// On submit click change background color.
$('.submit-button').click(function(e) {
e.preventDefault();
var selectedValue = $(".start-time option:selected").text(),
selectedEndValue = $(".end-time option:selected").text(),
$name = $('input[name=txtname]').val();
if($('#meeting').val()) {
$(".meeting-timetable tr > td").each(function() {
if ($(this).text() === selectedValue) {
$(this).parent('tr').addClass('active');
}
if ($(this).text() === selectedEndValue) {
var $parent = $(this).parent('tr').prevUntil('tr.active').addClass('active');
}
// Prev all td selected.
$($parent).each(function(){
});
});
}
// Form validation.
if ($('#meeting').val() === '') { $('.meeting-error').fadeIn(); }
// Grab form values
var formData = {
'txtrname' : $('input[name=txtrname]').val(),
'txtname' : $('input[name=txtname]').val(),
'txtemail' : $('input[name=txtemail]').val(),
'txtpurpose' : $('input[name=txtpurpose]').val(),
'attendee' : $('select[name=attendee]').val(),
'txtdate' : $('input[name=txtdate]').val(),
'btime' : $('select[name=btime]').val(),
'etime' : $('select[name=etime]').val()
};
if($('#meeting').val().trim()) {
// Ajax form submit.
$.ajax({
type: "POST",
url: "insertkarma.php",
data: formData,
success: function()
{
$('#alert-msg').fadeIn();
$('#plan').fadeOut();
}
});
} else { alert('Please enter the purpose of meeting'); } });
// on focus function
$('#meeting').focus(function() {
$('.meeting-error').fadeOut();
}).blur(function() {
if (!$(this).val()) {
$('.meeting-error').fadeIn();
}
});
$(".close").click(function() {
$('.pop-up').fadeOut();
});
// Adding calender.
$( "#datepicker" ).datepicker({
minDate: 0,
dateFormat: "yy-mm-dd",
onSelect: function (date) {
var url="karma.php?selecteddate="+date;
window.location.assign(url);
}
});
});
</script>
</head>
<body background="a9.jpg">
<img id="img" src="HITECHLOGO.jpg" width="150px" height="100px">
<h1 align="center" style="position:absolute;top:30px; left:600px;">KARMA <br>(3-SEATER)</h1>
<div id="heading">Home <?php echo ucwords($_SESSION['usr_name']); ?></font> Change Password Logout Help</div>
<hr width="100%">
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testmra"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$i = 0;
$dateEntry = "SELECT `starttime` , `status_id` , `name` , floor(( time_to_sec(`endtime`)-time_to_sec(`starttime`) )/1800) as testing FROM `karmadetails` WHERE `date` = '$selecteddate' order by starttime";
if ($result=mysqli_query($conn,$dateEntry)) {
while ($obj=mysqli_fetch_object($result))
{
$starttime[$i] = $obj->starttime;
$status[$i] = $obj->status_id;
$name[$i] = $obj->name;
$testing[$i] = $obj->testing;
$i++;
}
}
mysqli_close($conn);
?>
<table border="1" align="right" width="60%" style="border:black;">
<tr><td id="td" colspan="3" align="center"><h1>Click to Book a Time Slot</h1></td></tr>
<tr><td bgcolor="red" align="center" id="td">Booked</td><td align="center" id="td">Available</td></td></tr>
<tr><td id="td" colspan="3">| Today |</td></tr></table>
<table class="meeting-timetable" border="1" align="right" width="60%" style="border:black;">
<tr><th id="td" colspan="2" class="calender-date" align="center"><?php print $selecteddate; ?></th></tr>
<tr><td id="08:00:00" align="center" style="border-radius: 5px;border-style: solid;" width="10%">08:00</td><td id="td"></td></tr>
<tr><td id="08:30:00" align="center" style="border-radius: 5px;border-style: solid;">08:30</td><td id="td"></td></tr>
<tr><td id="09:00:00" align="center" style="border-radius: 5px;border-style: solid;">09:00</td><td id="td"></td></tr>
<tr><td id="09:30:00" align="center" style="border-radius: 5px;border-style: solid;">09:30</td><td id="td"></td></tr>
<tr><td id="10:00:00" align="center" style="border-radius: 5px;border-style: solid;">10:00</td><td id="td"></td></tr>
<tr><td id="10:30:00" align="center" style="border-radius: 5px;border-style: solid;">10:30</td><td id="td"></td></tr>
<tr><td id="11:00:00" align="center" style="border-radius: 5px;border-style: solid;">11:00</td><td id="td"></td></tr>
<tr><td id="11:30:00" align="center" style="border-radius: 5px;border-style: solid;">11:30</td><td id="td"></td></tr>
<tr><td id="12:00:00" align="center" style="border-radius: 5px;border-style: solid;">12:00</td><td id="td"></td></tr>
<tr><td id="12:30:00" align="center" style="border-radius: 5px;border-style: solid;">12:30</td><td id="td"></td></tr>
<tr><td id="13:00:00" align="center" style="border-radius: 5px;border-style: solid;">13:00</td><td id="td"></td></tr>
<tr><td id="13:30:00" align="center" style="border-radius: 5px;border-style: solid;">13:30</td><td id="td"></td></tr>
<tr><td id="14:00:00" align="center" style="border-radius: 5px;border-style: solid;">14:00</td><td id="td"></td></tr>
<tr><td id="14:30:00" align="center" style="border-radius: 5px;border-style: solid;">14:30</td><td id="td"></td></tr>
<tr><td id="15:00:00" align="center" style="border-radius: 5px;border-style: solid;">15:00</td><td id="td"></td></tr>
<tr><td id="15:30:00" align="center" style="border-radius: 5px;border-style: solid;">15:30</td><td id="td"></td></tr>
<tr><td id="16:00:00" align="center" style="border-radius: 5px;border-style: solid;">16:00</td><td id="td"></td></tr>
<tr><td id="16:30:00" align="center" style="border-radius: 5px;border-style: solid;">16:30</td><td id="td"></td></tr>
<tr><td id="17:00:00" align="center" style="border-radius: 5px;border-style: solid;">17:00</td><td id="td"></td></tr>
<tr><td id="17:30:00" align="center" style="border-radius: 5px;border-style: solid;">17:30</td><td id="td"></td></tr>
<tr><td id="18:00:00" align="center" style="border-radius: 5px;border-style: solid;">18:00</td><td id="td"></td></tr>
<tr><td id="18:30:00" align="center" style="border-radius: 5px;border-style: solid;">18:30</td><td id="td"></td></tr>
<tr><td id="19:00:00" align="center" style="border-radius: 5px;border-style: solid;">19:00</td><td id="td"></td></tr>
<tr><td id="19:30:00" align="center" style="border-radius: 5px;border-style: solid;">19:30</td><td id="td"></td></tr>
<tr><td id="20:00:00" align="center" style="border-radius: 5px;border-style: solid;">20:00</td><td id="td"></td></tr>
</table>
<table></table>
<?php
$name=$_SESSION['usr_name'];
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$dbname="testmra"; // Database name
// Create connection
$conn = mysqli_connect($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
}
$res = mysqli_query($conn,"SELECT emailid FROM users WHERE username='$name'");
while($row=mysqli_fetch_assoc($res))
{
$rows = $row['emailid'];
}
?>
<div id="plan" class="pop-up" style="display :none ">
<span class="close">Close</span>
<form align="center" method="post" id="inform">
<h1 align="center">Meeting Details</h1>
<table>
<tr><td align="right"><b>Room : </td><td><input type="text" name="txtrname" value="Karma" readonly></td></tr>
<tr><td align="right"><b>Name :</td><td><input type="text" name="txtname" readonly value="<?php echo ucwords($_SESSION['usr_name']); ?>" ></td></tr>
<tr><td align="right"><b>Email Id :</td><td><input type="text" name="txtemail" readonly value="<?php echo $rows ?>"></td></tr>
<tr><td align="right"><b>Purpose of Meeting :</td><td> <input id="meeting" type="text" name="txtpurpose"><span class="meeting-error">Enter the purpose of meeting</span></td></tr>
<tr><td align="right"><b>No. of Attendee :</td><td><select name="attendee"><option value="2">2</option><option value="3">3</option></select></td></tr>
<tr><td align="right"><b>Date :</td><td> <input class="date" type="text" name="txtdate" readonly value="yyyy/mm/dd"></td></tr>
<tr><td align="right"><b>Time : </td><td>Start Time <select class="start-time" name="btime">
<option value="08:00:00">08:00</option>
<option value="08:30:00">08:30</option>
<option value="09:00:00">09:00</option>
<option value="09:30:00">09:30</option>
<option value="10:00:00">10:00</option>
<option value="10:30:00">10:30</option>
<option value="11:00:00">11:00</option>
<option value="11:30:00">11:30</option>
<option value="12:00:00">12:00</option>
<option value="12:30:00">12:30</option>
<option value="13:00:00">13:00</option>
<option value="13:30:00">13:30</option>
<option value="14:00:00">14:00</option>
<option value="14:30:00">14:30</option>
<option value="15:00:00">15:00</option>
<option value="15:30:00">15:30</option>
<option value="16:00:00">16:00</option>
<option value="16:30:00">16:30</option>
<option value="17:00:00">17:00</option>
<option value="17:30:00">17:30</option>
<option value="18:00:00">18:00</option>
<option value="18:30:00">18:30</option>
<option value="19:00:00">19:00</option>
<option value="19:30:00">19:30</option>
<option value="20:00:00" disabled>20:00</option>
</select>
- End Time <select class="end-time" name="etime">
<option value="08:00:00">08:00</option>
<option value="08:30:00">08:30</option>
<option value="09:00:00">09:00</option>
<option value="09:30:00">09:30</option>
<option value="10:00:00">10:00</option>
<option value="10:30:00">10:30</option>
<option value="11:00:00">11:00</option>
<option value="11:30:00">11:30</option>
<option value="12:00:00">12:00</option>
<option value="12:30:00">12:30</option>
<option value="13:00:00">13:00</option>
<option value="13:30:00">13:30</option>
<option value="14:00:00">14:00</option>
<option value="14:30:00">14:30</option>
<option value="15:00:00">15:00</option>
<option value="15:30:00">15:30</option>
<option value="16:00:00">16:00</option>
<option value="16:30:00">16:30</option>
<option value="17:00:00">17:00</option>
<option value="17:30:00">17:30</option>
<option value="18:00:00">18:00</option>
<option value="18:30:00">18:30</option>
<option value="19:00:00">19:00</option>
<option value="19:30:00">19:30</option>
<option value="20:00:00">20:00</option>
</select>
</td></tr>
<tr></tr>
<tr><td></td><td><input class="submit-button" type="submit" value="Submit">
<!--<input class="reset-button" type="reset" value="Reset">--></td></tr>
</table>
</form>
</div>
<div id="plan-bg" class="pop-up"></div>
<div id="alert-msg" class="pop-up"><span class="close" onclick="window.location.reload();">Close</span><p>The selected time slot is booked.</p></div>
<div id="time-slot-msg" class="pop-up"><span class="close">Close</span><p>This time slot is already booked.</p></div>
<div id="datepicker"></div>
<?php
$user = ucwords($_SESSION['usr_name']);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testmra"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$result = mysqli_query($conn,"SELECT * FROM karmadetails WHERE name='$user' AND date='$selecteddate' AND (status_id=1 OR status_id=2)");
echo "<table border='1' style='border:black;'>
<tr>
<th id='td'>User Id</th>
<th id='td'>Purpose</th>
<th id='td'>Date</th>
<th id='td'>Start Time</th>
<th id='td'>End Time</th>
<th id='td'>Cancel Booking</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' id='td'>" . $row['id'] . </td>";
echo "<td align='center' id='td'>" . $row['purpose'] . "</td>";
echo "<td align='center' id='td'>" . $row['date'] . "</td>";
echo "<td align='center' id='td'>" . $row['starttime'] . "</td>";
echo "<td align='center' id='td'>" . $row['endtime'] . "</td>";
echo '<td align="center" id="td"> <a href="cancelkarma.php?userid='. $row['id'].'" style="color:red" onclick="return confirm(\'Are you sure you want to cancel the booking ?\')" >Cancel</a> </td>';
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script type="text/javascript">
var jqueryarray = [];
jqueryarray = <?php echo json_encode($starttime); ?>;
var statusarray=[];
statusarray = <?php echo json_encode($status); ?>;
var namearray=[];
namearray = <?php echo json_encode($name); ?>;
var countarray = [];
countarray = <?php echo json_encode($testing); ?>;
var arr = [];
for(i = 0; i < <?php echo $i; ?>; i++)
{
arr[i] = jqueryarray[i];
}
var brr = [];
for(i = 0; i < <?php echo $i; ?>; i++)
{
brr[i] = statusarray[i];
}
var crr=[];
for(i=0; i< <?php echo $i; ?>; i++)
{
crr[i] = namearray[i];
}
var drr=[];
for(i=0; i< <?php echo $i; ?>; i++)
{
drr[i] = countarray[i];
}
var j = 0;
var k=1;
var currentrow;
$('.meeting-timetable tr').each(function() {
if(arr[j] == $(this).find('td').attr('id'))
{
if(brr[j]==1)
{
currentrow=$(this);
for(k=1;k<=parseInt(countarray[j]);k++)
{
currentrow.find('td').next().css('background-color','orange').addClass('active').attr("title",namearray[j]);
currentrow=currentrow.next('tr');
}
}
if(brr[j]==2)
{
currentrow=$(this);
for(k=1;k<=parseInt(countarray[j]);k++)
{
currentrow.find('td').next().css('background-color','red').addClass('active').attr("title",namearray[j]);
currentrow=currentrow.next('tr');
}
}
if(brr[j]==3)
{
$(this).find('td').next().css('background-color','');
}
if(brr[j]==4)
{
$(this).find('td').next().css('background-color','');
}
j++;
}
});
</script>
</body>
</html>
insert.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testmra"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$room = mysqli_real_escape_string($conn, $_POST['txtrname']);
$name = mysqli_real_escape_string($conn, $_POST['txtname']);
$email = mysqli_real_escape_string($conn, $_POST['txtemail']);
$purpose = mysqli_real_escape_string($conn, $_POST['txtpurpose']);
$attendee = mysqli_real_escape_string($conn, $_POST['attendee']);
$date = mysqli_real_escape_string($conn, $_POST['txtdate']);
$btime = mysqli_real_escape_string($conn, $_POST['btime']);
$etime = mysqli_real_escape_string($conn, $_POST['etime']);
$sql="INSERT INTO karmadetails (room,name,purpose,attendee,date,starttime,endtime,status_id)VALUES('$room','$name','$purpose','$attendee','$date','$btime','$etime','2')";
$to = '$email';
$subject = "Your Booking is Done!!!";
$message = "<p>Hello $name . You've received this E-Mail because you have booked room $room from Timings $btime to $etime on Date $date.";
$from = "kansaramankit#gmail.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
if (mysqli_query($conn,$sql))
{
echo "Record added";
}
else
{
die('Error: ' . mysqli_error());
}
?>

Related

Editing MySQL row via POST form

I've encountered a problem which i'll try to describe below...
Let's say i've got a button for each table row (table is created by acquiring the data from the database...). the button sends a massive of values via post.
<button value='$e->eventId|$e->eventType|$e->eventDate|$e->eventPrice|$e->eventDescr' name='editValue'>EDIT</button>
I've written a function to explode the values of the massive and fill the inputs on the same page.
if(!empty($_POST['editValue'])) {
$editValue_fill = explode("|", $_POST["editValue"]);
$eventId = $editValue_fill[0];
$eventType = $editValue_fill[1];
$eventDate = $editValue_fill[2];
$eventPrice = $editValue_fill[3];
$eventDescr = $editValue_fill[4];
}
Next comes the function to edit the row in the database.
function editEvent ($id, $type, $date, $price, $descr){
$mysqli = new mysqli($GLOBALS["serverHost"], $GLOBALS["serverUsername"], $GLOBALS['serverPassword'], $GLOBALS['dbName']);
$stmt = $mysqli->prepare("UPDATE events_archive SET eventType='?', eventDate='?', eventPrice='?', eventDescr='?' WHERE id='?'");
$stmt->bind_param("ssdss", $type, $date, $price, $descr, $id);
$stmt->execute();
header("Refresh:0");
if($stmt->execute()){
$eventNotice="Event successfully updated!";
}else{
$eventNotice = "Failed to save...";
}
return $eventNotice;
}
And, of course, the usage of the function which apparently doesnt work. The page just refreshes and nothing happens.
if(empty($eventTypeError)&& empty($eventDateError)&& empty($eventPriceError) && empty($eventDescrError)
&& isset($_POST['eventType']) && isset($_POST['eventDate']) && isset ($_POST['eventPrice']) && isset
($_POST['eventDescr']) && !empty($eventId)){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}
So basically, gets values from the row -> fills them into inputs in the same page -> if $eventId is set then updates the row, if not - creates a new row (diff. function)
Could anyone give me a tip or help solving the problem? I've been trying to understand the issue and failed dramatically...
<?php
require ("functions.php");
var_dump($_POST);
//kas on sisseloginud, kui ei ole siis
//suunata login lehele
//kas ?logout on aadressireal
if (isset($_GET['logout'])){
session_destroy();
header("Location: login.php");
}
if (!isset ($_SESSION["userId"])){
header("Location: login.php");
}
$confirm = "";
$eventNotice = "";
$eventTypeError = "";
$eventDateError = '';
$eventPriceError = '';
$eventDescrError = '';
$eventType = '';
$eventDescr = '';
$eventPrice = '';
$eventDate = date("Y-m-d");
if (isset ($_POST["eventType"])){
if (empty($_POST['eventType'])){
$eventTypeError = "Please choose the event type!";
} else {
$eventType = $_POST["eventType"];
}
}
if (isset ($_POST ["eventDate"])){
if (empty ($_POST ["eventDate"])){
$eventDateError = "Please choose the date!";
} else {
$eventDate = $_POST["eventDate"];
}
}
if (isset ($_POST ["eventPrice"])){
if (empty ($_POST ["eventPrice"])){
$eventPriceError = "Please type in the price!";
} else {
$eventPrice = $_POST["eventPrice"];
}
}
if (isset ($_POST ["eventDescr"])){
if (empty ($_POST ["eventDescr"])){
$eventDescrError = "Please type in the description!";
}elseif (strlen($_POST["eventDescr"])< 10) {
$eventDescrError = "Description must be longer than 10 symbols!";
$eventDescr = $_POST['eventDescr'];
}else{
$eventDescr = $_POST['eventDescr'];
}
}
$event = getAllEvents();
if(!empty($_POST['delValue'])) {
delEvent($_POST['delValue']);
}
if(!empty($_POST['editValue'])) {
$editValue_fill = explode("|", $_POST["editValue"]);
$eventId = $editValue_fill[0];
$eventType = $editValue_fill[1];
$eventDate = $editValue_fill[2];
$eventPrice = $editValue_fill[3];
$eventDescr = $editValue_fill[4];
echo ($eventId);
echo ($eventDate);
echo ($eventPrice);
echo ($eventType);
echo ($eventDescr);
}
if(empty($eventTypeError)&& empty($eventDateError)&& empty($eventPriceError) && empty($eventDescrError)
&& isset($_POST['eventType']) && isset($_POST['eventDate']) && isset ($_POST['eventPrice']) && isset
($_POST['eventDescr'])){
if(isset($_POST['editValue'])){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}elseif(!isset($_POST['editValue'])){
$eventNotice = newEvent(cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}
}
?>
<html>
<style>
#import "styles.css";
ul.tab {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
ul.tab li {float: left;}
ul.tab li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.tab li a:hover {
background-color: #ddd;
}
ul.tab li a:focus, .active {
background-color: #ccc;
}
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
</style>
<body>
<form method ="post">
<table class="table1">
<tr>
<td style="text-align:center"><h1>Data</h1></td>
</tr>
<tr>
<th><h2>Profile</h2></th>
</tr>
<tr>
<td>
<table class="table2">
<tr>
<td colspan="3"">Welcome <?=$_SESSION['email'];?>!</td>
</tr>
<tr>
<td colspan="3" style="text-align:center">Log out</td>
</tr>
</table>
<tr>
<td>
<tr>
<td>
<ul class="tab">
<li>Add/edit</li>
<li>Archive</li>
</ul>
<div id="Add/edit" class="tabcontent">
<input type="hidden" value='eventId'>
<table class="table2">
<tr>
<td>Event type:<span class = 'redtext'>*</span></td>
<td style="text-align:left">
<select name="eventType">
<?php if(empty($eventType)){?>
<option value="" selected>Choose here</option>
<?php } else { ?>
<option value="">Choose here</option>
<?php } ?>
<?php if($eventType == "Planned service"){?>
<option value="Planned service" selected>Planned service</option>
<?php } else { ?>
<option value="Planned service">Planned service</option>
<?php } ?>
<?php if($eventType == "Unplanned service"){?>
<option value="Unplanned service" selected>Unplanned service</option>
<?php } else { ?>
<option value="Unplanned service">Unplanned service</option>
<?php } ?>
<?php if($eventType == "Fuel checks"){?>
<option value="Fuel checks" selected>Fuel checks</option>
<?php } else { ?>
<option value="Fuel checks">Fuel checks</option>
<?php } ?>
<?php if($eventType == "Tuning"){?>
<option value="Tuning" selected>Tuning</option>
<?php } else { ?>
<option value="Tuning">Tuning</option>
<?php } ?>
<?php if($eventType == "Car accident"){?>
<option value="Car accident" selected>Car accident</option>
<?php } else { ?>
<option value="Car accident">Car accident</option>
<?php } ?>
</select>
</td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventTypeError?></td></tr>
<tr>
<td>Date:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><input name="eventDate" type ="date" min="1900-01-01" max = "<?=date('Y-m-d'); ?>" value = "<?=$eventDate?>" placeholder="YYYY-MM-DD"></td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventDateError?></td></tr>
<tr>
<td>Price:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><input type="text" name="eventPrice" placeholder="ex. 15.50" onkeypress="return onlyNumbersWithDot(event);" / value = "<?=$eventPrice?>"></td>
<script type="text/javascript">
function onlyNumbersWithDot(e) {
var charCode;
if (e.keyCode > 0) {
charCode = e.which || e.keyCode;
}
else if (typeof (e.charCode) != "undefined") {
charCode = e.which || e.keyCode;
}
if (charCode == 46)
return true
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventPriceError?></td></tr>
<tr>
<td>Description:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><textarea name="eventDescr" cols="50" rows="10" placeholder="Describe event here..."><?=$eventDescr?></textarea></td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventDescrError?></td></tr>
<tr>
<td colspan="3" style="text-align:center"><button type ="submit" value = "Submit">Save</button></td>
</tr>
<tr>
<td colspan="3" style="text-align:center"><p class = "redtext"><?=$eventNotice;?></p></td>
</tr>
</table>
</div>
</form>
<form method="post">
<div id="Archive" class="tabcontent">
<table class="table2">
<tr>
<td colspan="3"">
<?php
$html = "<table>";
$html .= "<tr>";
$html .= "<th>Event type</th>";
$html .= "<th>Date</th>";
$html .= "<th>Price(€)</th>";
$html .= "<th>Description</th>";
$html .= "<th>Delete</th>";
$html .= "<th>Edit</th>";
$html .= "</tr>";
foreach($event as $e){
$html .= "<tr>";
$html .= "<td>$e->eventType</td>";
$html .= "<td>$e->eventDate</td>";
$html .= "<td>$e->eventPrice</td>";
$html .= "<td>$e->eventDescr</td>";
$html .= "<td><button style='border:none; background-color: transparent;' value='$e->eventId' name='delValue' onclick=\"return confirm('Do you really want to delete this row?')\"><img src='delete.png' width='20' height='20'></button></td>";
$html .= "<td><button style='border:none; background-color: transparent;' value='$e->eventId|$e->eventType|$e->eventDate|$e->eventPrice|$e->eventDescr' name='editValue'><img src='edit.png' width='20' height='20'></button></td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
?>
</td>
</tr>
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
</td>
</tr>
</table>
</td>
</tr>
</div>
</form>
</body>
</html>
Looking forward to Your help!
Best regards :)
if(isset($_POST['editValue'])){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}

How to retain data (i.e color) on refresh or login of another user?

I am new to php.. I have made a page where a user can login in & book a room for meeting. I used tables to display the time slot. On click of a time slot a form appears & where user inputs the details. On click of submit the data is inserted into db .But if the page is refreshed the color in the table disappears or if any other user logins in the color is not present.
<?php
session_start();
?>
<html>
<head><title>MRA</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<style>
.name-error,
.meeting-error
{
color: red;
margin: 0 10px;
}
#plan
{
position: fixed;
left: 50%;
margin-left: -239px;
top: 50%;
margin-top: -150px;
z-index: 10;
background-color: #fff;
padding: 10px;
}
#plan-bg
{
width: 100%;
height: 100%;
position: fixed;
background: #000;
opacity: 0.8;
top: 0;
display: none;
z-index: 5;
}
.close
{
position: absolute;
top: 5px;
right: 10px;
cursor: pointer;
}
.close:hover
{
text-decoration: underline;
}
#datepicker
{
width: 30%;
float: left;
}
.calender-date
{
padding: 0 10px;
}
#time-slot-msg,
#alert-msg
{
position: fixed;
top: 50%;
left: 50%;
background: #fff;
font-size: 25px;
padding: 10px 20px;
margin: -50px 0 0 -184.5px;
display: none;
z-index: 10;
border-radius: 5px;
}
#time-slot-msg p,
#alert-msg p { font-weight: bold; }
#time-slot-msg .close,
#alert-msg .close
{
font-size: 15px;
right: 20px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
var $index;
$('.meeting-error').hide();
$(".meeting-timetable tr > td + td").click(function(e) {
if($(this).hasClass('active')) {
e.preventDefault();
alert('The selected time slot has already been assigned');
} else {
var $thisVal = $(this).prevAll('td:first-child').text();
$index = $(this).index();
// Start time.
$(".start-time option").each(function() {
if (($(this).text() === $thisVal)) {
$(this).attr('selected', 'selected');
$(this).prevAll().attr('disabled', 'disabled');
}
});
// Change end time.
$(".end-time option").each(function() {
if (($(this).text() === $thisVal)) {
$(this).next().attr('selected', 'selected');
$(this).attr('disabled', 'disabled');
$(this).prevAll().attr('disabled', 'disabled');
}
});
// Date input select.
var $date = $('.meeting-timetable tr:first-child th').text();
var dateFormat = $.datepicker.formatDate('yy-mm-dd', new Date($date));
$('.date').val(dateFormat);
$('#meeting').val('');
$('.meeting-error').hide();
// Show dialog box.
$('#plan, #plan-bg').fadeIn();
}
});
// On submit click change background color.
$('.submit-button').click(function(e) {
e.preventDefault();
var selectedValue = $(".start-time option:selected").text(),
selectedEndValue = $(".end-time option:selected").text(),
$name = $('input[name=txtname]').val();
if($('#meeting').val()) {
$(".meeting-timetable tr > td").each(function() {
if ($(this).text() === selectedValue) {
$(this).parent('tr').addClass('active');
var $active1 = $(this).nextAll().eq($index - 1).css('background-color', 'orange').addClass('active').attr("title", $name);
}
if ($(this).text() === selectedEndValue) {
$(this).nextAll().eq($index - 1).css('background-color', 'orange').addClass('active').attr("title", $name);
var $parent = $(this).parent('tr').prevUntil('tr.active').addClass('active');
}
// Prev all td selected.
$($parent).each(function(){
$(this).children('td').nextAll().eq($index - 1).css('background-color', 'orange').addClass('active').attr("title", $name);
});
});
}
// Form validation.
if ($('#meeting').val() === '') {
$('.meeting-error').fadeIn();
}
// Grab form values
var formData = {
'txtrname' : $('input[name=txtrname]').val(),
'txtname' : $('input[name=txtname]').val(),
'txtpurpose' : $('input[name=txtpurpose]').val(),
'attendee' : $('select[name=attendee]').val(),
'txtdate' : $('input[name=txtdate]').val(),
'btime' : $('select[name=btime]').val(),
'etime' : $('select[name=etime]').val()
};
if($('#meeting').val().trim()) {
// Ajax form submit.
$.ajax({
type: "POST",
url: "insert.php",
data: formData,
success: function()
{
$('#alert-msg').fadeIn();
$('#plan').fadeOut();
}
});
} else {
alert('Please enter the purpose of meeting');
}
});
// on focus function
$('#meeting').focus(function() {
$('.meeting-error').fadeOut();
}).blur(function() {
if (!$(this).val()) {
$('.meeting-error').fadeIn();
}
});
// Reset button functionality.
$('.reset-button').click(function() {
$('.inputDisabled').prop("disabled", false);
$(".start-time option, .end-time option").removeAttr('disabled', 'disabled');
$('.start-time option, .end-time option').removeAttr('selected', 'selected');
$('.end-time option:eq(0)').attr('disabled', 'disabled');
$('.end-time option:eq(1)').attr('selected', 'selected');
$('.meeting-error').hide();
});
$(".close").click(function() {
$('.pop-up').fadeOut();
});
$('.calender-date').html($.datepicker.formatDate('d MM, yy', new Date()));
// Adding calender.
$( "#datepicker" ).datepicker({
minDate: 0,
dateFormat: "d MM, yy",
onSelect: function (date) {
$('.calender-date').text(date);
}
});
});
</script>
</head>
<body bgcolor="#BEBCBC">
<h3 align="right">Home
<?php
echo ucwords($_SESSION['usr_name']);
?>
Logout</h3>
<table border="1" align="right" width="70%">
<tr><td colspan="3" align="center"><h1>Click to Book a Time Slot</h1></td> </tr>
<tr><td bgcolor="red" align="center">Booked</td><td align="center" bgcolor="orange">Requested</td><td align="center" bgcolor="Green">Available</td> </td></tr>
<tr><td colspan="3">| Today |</td></tr></table>
<table class="meeting-timetable" border="1" align="right" width="70%">
<tr><th colspan="2" class="calender-date" align="center"></th></tr>
<tr><td align="center" width="10%">8:00 a.m</td><td></td></tr>
<tr><td align="center">8:30 a.m</td><td></td></tr>
<tr><td align="center">9:00 a.m</td><td></td></tr>
<tr><td align="center">9:30 a.m</td><td></td></tr>
<tr><td align="center">10:00 a.m</td><td></td></tr>
<tr><td align="center">10:30 a.m</td><td></td></tr>
<tr><td align="center">11:00 a.m</td><td></td></tr>
<tr><td align="center">11:30 a.m</td><td></td></tr>
<tr><td align="center">12:00 p.m</td><td></td></tr>
<tr><td align="center">12:30 p.m</td><td></td></tr>
<tr><td align="center">1:00 p.m</td><td></td></tr>
<tr><td align="center">1:30 p.m</td><td></td></tr>
<tr><td align="center">2:00 p.m</td><td></td></tr>
<tr><td align="center">2:30 p.m</td><td></td></tr>
<tr><td align="center">3:00 p.m</td><td></td></tr>
<tr><td align="center">3:30 p.m</td><td></td></tr>
<tr><td align="center">4:00 p.m</td><td></td></tr>
<tr><td align="center">4:30 p.m</td><td></td></tr>
<tr><td align="center">5:00 p.m</td><td></td></tr>
<tr><td align="center">5:30 p.m</td><td></td></tr>
<tr><td align="center">6:00 p.m</td><td></td></tr>
<tr><td align="center">6:30 p.m</td><td></td></tr>
<tr><td align="center">7:00 p.m</td><td></td></tr>
<tr><td align="center">7:30 p.m</td><td></td></tr>
<tr><td align="center">8:00 p.m</td><td></td></tr>
</table>
<div id="plan" class="pop-up" style="display :none ">
<span class="close">Close</span>
<form align="center" method="post" id="inform">
<h1 align="center">Meeting Details</h1>
<table>
<tr><td><b>Room : </td><td><input type="text" name="txtrname" value="Karma" readonly></td></tr>
<tr><td><b>Name :</td><td><input type="text" name="txtname" readonly value=" <?php echo ucwords($_SESSION['usr_name']); ?>" ></td></tr>
<tr><td><b>Purpose of Meeting :</td><td> <input id="meeting" type="text" name="txtpurpose"></td></tr>
<tr><td><b>No. of Attendee :</td><td><select name="attendee"><option value="1">1</option><option value="2">2</option><option value="3">3</option> </select></td></tr>
<tr><td><b>Date :</td><td> <input class="date" type="text" name="txtdate" readonly value="yyyy/mm/dd"></td></tr>
<tr><td><b>Time : </td><td>Start Time <select class="start-time" name="btime">
<option value="8:00 a.m">8:00 a.m</option>
<option value="8:30 a.m">8:30 a.m</option>
<option value="9:00 a.m">9:00 a.m</option>
<option value="9:30 a.m">9:30 a.m</option>
<option value="10:00 a.m">10:00 a.m</option>
<option value="10:30 a.m">10:30 a.m</option>
<option value="11:00 a.m">11:00 a.m</option>
<option value="11:30 a.m">11:30 a.m</option>
<option value="12:00 p.m">12:00 p.m</option>
<option value="12:30 p.m">12:30 p.m</option>
<option value="1:00 p.m">1:00 p.m</option>
<option value="1:30 p.m">1:30 p.m</option>
<option value="2:00 p.m">2:00 p.m</option>
<option value="2:30 p.m">2:30 p.m</option>
<option value="3:00 p.m">3:00 p.m</option>
<option value="3:30 p.m">3:30 p.m</option>
<option value="4:00 p.m">4:00 p.m</option>
<option value="4:30 p.m">4:30 p.m</option>
<option value="5:00 p.m">5:00 p.m</option>
<option value="5:30 p.m">5:30 p.m</option>
<option value="6:00 p.m">6:00 p.m</option>
<option value="6:30 p.m">6:30 p.m</option>
<option value="7:00 p.m">7:00 p.m</option>
<option value="7:30 p.m">7:30 p.m</option>
<option value="8:00 p.m" disabled>8:00 p.m</option>
</select>
- End Time <select class="end-time" name="etime">
<option value="8:00 a.m">8:00 a.m</option>
<option value="8:30 a.m">8:30 a.m</option>
<option value="9:00 a.m">9:00 a.m</option>
<option value="9:30 a.m">9:30 a.m</option>
<option value="10:00 a.m">10:00 a.m</option>
<option value="10:30 a.m">10:30 a.m</option>
<option value="11:00 a.m">11:00 a.m</option>
<option value="11:30 a.m">11:30 a.m</option>
<option value="12:00 p.m">12:00 p.m</option>
<option value="12:30 p.m">12:30 p.m</option>
<option value="1:00 p.m">1:00 p.m</option>
<option value="1:30 p.m">1:30 p.m</option>
<option value="2:00 p.m">2:00 p.m</option>
<option value="2:30 p.m">2:30 p.m</option>
<option value="3:00 p.m">3:00 p.m</option>
<option value="3:30 p.m">3:30 p.m</option>
<option value="4:00 p.m">4:00 p.m</option>
<option value="4:30 p.m">4:30 p.m</option>
<option value="5:00 p.m">5:00 p.m</option>
<option value="5:30 p.m">5:30 p.m</option>
<option value="6:00 p.m">6:00 p.m</option>
<option value="6:30 p.m">6:30 p.m</option>
<option value="7:00 p.m">7:00 p.m</option>
<option value="7:30 p.m">7:30 p.m</option>
<option value="8:00 p.m">8:00 p.m</option>
</select>
</td></tr>
<tr><td></td>
<td><input class="submit-button" type="submit" value="Submit">
</td></tr>
</table>
</form>
</div>
<div id="plan-bg" class="pop-up"></div>
<div id="alert-msg" class="pop-up"><span class="close">Close</span><p>Your request has been sent to admin.</p></div>
<div id="time-slot-msg" class="pop-up"><span class="close">Close</span> <p>This time slot is already booked.</p></div>
<div id="datepicker"></div>
</body>
</html>
That is because you are not looking up the meeting data and adding the 'active' css class to the time-slots that are already booked when you build your table. It is no use storing the data if you do not then use it when required.
That means that you need a query in your code to return the bookings and a loop control to check if each time-slot has been booked. When it has, add the active css class to that td.
<td class="active">
So modify your code to retrieve the bookings and ask more questions when you have problems with the new code.
PS instead of adding the background-color directly, define it as the background-color of the active class. Then just adding the class will produce the desired effect.

I need to insert data into mysql from a dynamic table with selects created with PHP

Wondering if someone could help me update mysql table with data from a couple of selects created dynamically with PHP, I have created the following code, but it seems not to be working, really appreciate your help:
`include_once('../includes/connection.php');`
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con);
if($con){
$registry = mysqli_affected_rows($connection);
if($registry > 0){
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="approveReject.php" method="post" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if($color==1){
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="1";
}
echo '
<form action="" method="post">
<input type="hidden" value="'.$registry['id'].'" name="id[]" id="id">
<th div align="center">'.$registry['title'].'
<td div align="center">'.$registry['details'].'
<td div align="center">'.$registry['category'].'
<td div align="center">'.$registry['start'].'
<td div align="center">'.$registry['end'].'
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<td><input type="submit" name="button" id="button" value="Submit" style="height:1.8em; width:7.3em;" /></td>
<?php
echo '</form>
';
echo '
</form>
</table>';
}
}else{
echo '<h1>There are no new requests to be approved</h1>';
}
if(isset($_POST['Submit'])){
if($_POST['allDay'] == 'Yes'){
$allDay = 'true';
}else{
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$i = 0;
foreach($_POST['id'] as $id){
$udpate_qry = "UPDATE evenement SET allDay='".$allDay."', approved='".$ap_re."', WHERE id='".$id."'";
$result_udpate_qry = mysqli_query($connection, $update_qry);
$i++;
}
}
?>
I don't think that I have solved all your issues, but this might help you further.
<?php
include_once('../includes/connection.php');
if (isset($_POST['submit'])) {
if ($_POST['allDay'] == 'Yes') {
$allDay = 'true';
} else {
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$id = $_POST['id'];
$i = 0;
while ($id) {
$update_qry = "UPDATE evenement SET allDay='" . $allDay . "', approved='".$ap_re[$i]. "' WHERE id='".$id[$i]."'";
echo var_dump($update_qry);
mysqli_query($connection, $update_qry) OR DIE(mysqli_error($connection));
$i++;
}
header('Location: http://www.stockoverflow.com/'); // INSERT approveReject.php ???
}
else {
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con) or die(mysqli_error($connection));
if ($result) {
$registry = mysqli_num_rows($result) or die (mysqli_error($connection));
if ($registry > 0) {
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="" method="POST" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while ($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($color == 1) {
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "1";
}
echo '
<input type="hidden" value="' . $registry['id'] . '" name="id[]" id="id">
<th div align="center">' . $registry['title'] . '
<td div align="center">' . $registry['details'] . '
<td div align="center">' . $registry['category'] . '
<td div align="center">' . $registry['start'] . '
<td div align="center">' . $registry['end'] . '
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<input type="submit" name="submit" id="button" value="Submit" style="height:1.8em; width:7.3em;" />
<?php
echo '</form>
';
echo '
</form>
</table>';
}
} else {
echo '<h1>There are no new requests to be approved</h1>';
}
}
?>
You should try this, your update syntax might causing you the problem
$udpate_qry = "UPDATE evenement
SET allDay='$allDay', approved='$ap_re'
WHERE id='$id'";
Mr. Radical, you are the man, I was able to update my table with your suggested code, I just needed to add $length = count($allDay) and then change the while loop to while(($id) && ($i < $length)) that made it. Really appreciate your help.

Insert and update text into sql

I have a page that seems to work fine other than updating new text to the database. Everytime I hit submit it just takes it back to the original data. Please let me know what is missing for this to successfully query old data and update new data.
if (isset($_POST["submit"]) && $_POST["submit"] == "Update Load")
{
for ($count = 1; $count <= 6; $count++)
{
$fields[$count] = "";
if (isset($_POST["field" . $count . ""]))
{
$fields[$count] = trim($_POST["field" . $count . ""]);
//echo $fields[$count] . "<br />";
}
}
$con = mysql_connect("", "", "");
mysql_select_db("", $con);
$carriername = mysql_real_escape_string($_POST['carriername']);
$contact = mysql_real_escape_string($_POST['contact']);
$phone = mysql_real_escape_string($_POST['phone']);
$rating = mysql_real_escape_string($_POST['rating']);
$info = mysql_real_escape_string($_POST['info']);
$insert = "UPDATE carrierinfo Set `carriername` = '$carriername', `contact` = '$contact', `phone` = '$phone', `rating` = '$rating', `info` = '$info' WHERE `id` = '$id';";
mysql_query($insert) or die(mysql_error());
$select = "SELECT `carriername` ,`contact` ,`phone` ,`rating` ,`info` ,`id` FROM `carrierinfo` ORDER BY `carriername` DESC;";
$result = mysql_query($select) or die(mysql_error());
}
if ($rating == "")
{
$rating = "3";
}
if (isset($_GET["id"]))
{
$con = mysql_connect("", "", "");
mysql_select_db("", $con);
$id = mysql_real_escape_string($_GET["id"]);
$select = "SELECT * FROM `carrierinfo` WHERE `id` = '$id'";
$result = mysql_query($select) or die(mysql_error());
$fields = mysql_fetch_array($result, MYSQL_BOTH);
mysql_close($con);
}
else
{
header("Location:board.php");
}
?>
</script>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<div
style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
<div
style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
<table style="font-weight: normal; width: 100%; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<table
style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<tbody>
<tr>
<td style="width: 35%;">Carrier Name:</td><td><input id="carriername" name="carriername" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[carriername]; ?>">
</tr>
<tr>
<td style="width: 35%;">Contact:</td><td><input id="contact" name="contact" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[contact]; ?>">
</tr>
<tr>
<td style="width: 35%;">Phone:</td><td><input id="phone" name="phone" maxlength="50" style="width: 100%;" type="text" value="<?php echo $fields[phone]; ?>">
</tr>
<tr>
<td style="width: 10%;">Carrier Rating:</td><td>
<select id="rating" name="rating">
<option value=""></option>
<option <?php if($fields[rating] == "1") echo "selected"; ?> value="1">1</option>
<option <?php if($fields[rating] == "2") echo "selected"; ?> value="2">2</option>
<option <?php if($fields[rating] == "3") echo "selected"; ?> value="3">3</option>
<option <?php if($fields[rating] == "4") echo "selected"; ?> value="4">4</option>
<option <?php if($fields[rating] == "5") echo "selected"; ?> value="5">5</option>
</select>
</td>
</tr>
<tr>
<td style="width: 10%;">Carrier Info:</td><td style="text-align: center;" colspan="2"><textarea name="info" maxlength="65535" style="width: 100%; height: 4em;"><?php echo $fields[info]; ?></textarea></td>
</tr>
</tbody>
</table>
<p style="text-align: center;"><input name="submit" value="Update"
class="submit" type="submit"></p>
</div>
</form>
<input type="button" onclick="window.location.href='test3.php';" value="Back" />
</div>
<p style="margin-bottom: -20px;"> </p>
</body>
$insert = "UPDATE carrierinfo Set `carriername` = '$carriername', `contact` = '$contact', `phone` = '$phone', `rating` = '$rating', `info` = '$info' **WHERE `id` = '$id';**";
You haven't declared what $id should be before running your UPDATE query so nothing is being updated.
You are looking for
$_POST["submit"] == "Update Load"
but the value of your submit button is "Update", not "Update Load".

Using MYSQL and dropdowns

Ok I almost got a dropdown pulling from my DB and posting to it as well to work. I got it to pull down the data and for it to submit to the DB. Still a stump. If I have example "ABC Trucking" as an option. It only posts "ABC" to table1. For whatever reason it doenst post two words? Any Ideas? See where the carriername dropdown is in the div.
My Code:
<?php
if (isset($_POST["submit"]) && $_POST["submit"] == "Submit")
{
for ($count = 1; $count <= 9; $count++)
{
$fields[$count] = "";
if (isset($_POST["field" . $count . ""]))
{
$fields[$count] = trim($_POST["field" . $count . ""]);
//echo $fields[$count] . "<br />";
}
}
$con = mysql_connect("local", "user", "pass");
mysql_select_db("DB", $con);
$carriername = mysql_real_escape_string($_POST['carriername']);
$fromzip = mysql_real_escape_string($_POST['fromzip']);
$tozip = mysql_real_escape_string($_POST['tozip']);
$typeofequipment = mysql_real_escape_string($_POST['typeofequipment']);
$weight = mysql_real_escape_string($_POST['weight']);
$length = mysql_real_escape_string($_POST['length']);
$paymentamount = mysql_real_escape_string($_POST['paymentamount']);
$contactperson = mysql_real_escape_string($_POST['contactperson']);
$loadtype = mysql_real_escape_string($_POST['loadtype']);
$insert = "INSERT INTO table1 (`carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype`) VALUES('$carriername' ,'$fromzip' ,'$tozip' ,'$typeofequipment' ,'$weight' ,'$length' ,'$paymentamount' ,'$contactperson' ,'$loadtype');";
mysql_query($insert) or die(mysql_error());
$select = "SELECT `carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` FROM `table1` ORDER BY `paymentamount` DESC;";
$result = mysql_query($select) or die(mysql_error());
}
?>
</script>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<div
style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
<div
style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
<table style="font-weight: normal; width: 100%; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<table
style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
<tbody>
<tr>
<td style="width: 10%;">Carrier:</td><td>
<?php
$con = mysql_connect("local", "user", "pass");
mysql_select_db("DB", $con);
$query=("SELECT * FROM table2");
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
echo "<select name=carriername>";
while($row=mysql_fetch_array($result)){
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td style="width: 35%;">Pick Zip:</td><td> <input id="fromzip" name="fromzip" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Drop Zip:</td><td> <input id="tozip" name="tozip" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Load Type:</td><td> <input id="loadtype" name="loadtype" maxlength="50"
style="width: 100%;" type="text">
</tr>
<tr>
<td style="width: 35%;">Rate:</td><td> <input id="paymentamount" name="paymentamount" maxlength="50"
style="width: 100%;" type="text">
</tr>
</tbody>
</table>
<p style="text-align: center;"><input name="submit" value="Submit"
class="submit" type="submit"></p>
</div>
</form>
</div>
<p style="margin-bottom: -20px;"> </p>
</body>
instead of :
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
use this
echo "<OPTION VALUE='".$row['carriername']."'>".$row['carriername']."</OPTION>";
notice ' in ur value...concate '' to your value attr.. so that it makes it a string....
EDITED
echo "<select name='carriername'>";
while($row=mysql_fetch_array($result)){
echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>";
}
echo "</select>";

Categories