Using MYSQL and dropdowns - php

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>";

Related

Undefined index: sessionName

Hello I have created a login page which is
<form method="POST" action="log.php">
<table border="1" cellpadding="4" cellspacing="0"
style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
width="100%">
<thead>
<tr>
<td style="border: 0px; width: 20%; background-color: transparent;">
Username:</td>
<td style="border: 0px; width: 30%; background-color: transparent;">
<input type="text" name="email" id="email" class="form-control"
autocomplete="off" required="">
</td>
</tr>
</thead>
</table>
<table border="1" cellpadding="4" cellspacing="0"
style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
width="100%">
<thead>
<tr>
<td style="border: 0px; width: 20%; background-color: transparent;">
Password:</td>
<td style="border: 0px; width: 30%; background-color: transparent;">
<input type="password" name="pass" id="pass" class="form-control"
autocomplete="off" required>
</td>
</tr>
</thead>
</table>
</div>
<div class="spacer-20"></div>
</div>
<!-- Social Signup -->
<div class="social-signup">
<span class="or-break"></span>
<center>
<button type="submit" name="login" class="btn btn-primary">
<span class="glyphicon glyphicon-log-in"></span> Login
</button>
No account? Sign up
</form>
and the login Processor is as follows
<?php
// Login Controller
// require('config/config.php');
include './config/config.php';
$userName = $passWord = $name = "";
if (isset ( $_POST ['login'] )) {
$userName = mysqli_real_escape_string ( $con, $_POST ['email'] );
$password = mysqli_real_escape_string ( $con, $_POST ['pass'] );
$pass = md5 ( $password );
// Block handles Doctor Login using the email
$get_doc = "SELECT * FROM doctor_registration WHERE email='$userName' AND password='$pass'";
$run_doc = mysqli_query ( $con, $get_doc );
$rows = mysqli_fetch_array ( $run_doc );
$docId = $rows ['doc_id'];
$docEmail = $rows ['email'];
$name = $rows ['first_name'] . " " . $rows ['last_name'];
if ($userName == $docEmail) {
$_SESSION ['sessionName'] = $name;
$_SESSION ['sessionId'] = $docId;
echo "<script>window.open('doctor_dashboard.php','_self')</script>";
} else {
// Block handles user Login using the username
$get_user = "SELECT * FROM users WHERE username='$userName' AND password='$pass'";
$run_user = mysqli_query ( $con, $get_user );
$rows = mysqli_fetch_array ( $run_user );
$userId = $rows ['user_id'];
$uname = $rows ['username'];
if ($userName == $uname) {
$_SESSION ['sessionName'] = $uname;
$_SESSION ['sessionId'] = $userId;
echo "<script>window.open('user_dashboard.php','_self')</script>";
} else {
echo "<script>alert('Passowrd or username is not correct!')</script>";
}
}
}
?>
and for the dashboard the processor checks the email or username and redirects to one of the dashboard.
i have two dashboards which have the code bellow for starting and storing a session
<?php
session_start ();
include './config/config.php';
include './updateFunction.php';
include './InsertFunction.php';
if (isset ( $_SESSION ['sessionName'] ) && ($_SESSION ['sessionId'])) {
header ( "location:index.php" );
}
?>
and each time i try to login in both dashboards i get the error:
Undefined index: sessionName
You should use session_start() before using $_SESSION['sessionName'] = $name
in the login processor.
So preferably you should use session_start() at the beginning of the page
Put session_start(); in your config.php after <?php
then call config.php on every page on after <?php
you are getting session error because you haven't use session_start(); in your file

How do I output this table correctly?

This code outputs the image on the left, and what I'm looking for is the image on the right: https://s27.postimg.org/niua66jqr/archive.jpg
How do I output this table correctly? Is it a problem with the archive code or the style code?
.archive-container {
margin-top: 85px;
}
.archive-container h2 {
font-weight: normal;
margin-bottom: 30px;
}
.archive,
.year,
th {
font-family: 'Georgia', sans-serif;
font-weight: normal;
color: #a6a6a6;
font-size: 16px;
}
.archive:hover th,
.year:hover th {
color: #a6a6a6 !important;
opacity: 1 !important;
}
.archive a:link:hover {
color: #e69900 !important;
}
a:link {
font-weight: normal;
color: black;
/
}
table {
margin-right: 20px;
border-spacing: 25px 3px;
}
<div class="archive-container">
<h2><?php the_title(); ?></h2>
<script type="text/javascript">
var domainroot = "sitetitle.com"
function Gsitesearch(curobj) {
curobj.q.value = "site:" + domainroot + " " + curobj.qfront.value
}
</script>
<form action="http://www.google.com/search" method="get" onSubmit="Gsitesearch(this)">
<p class="search">Search:
<br />
<input name="q" type="hidden" class="texta" />
<input name="qfront" type="text" style="width: 186px; text-size: 12px; height: 14px;" />
</p>
</form>
<table id=arc>
<?php $query="SELECT YEAR(post_date) AS `year`, MONTH(post_date) as `month`, DAYOFMONTH(post_date) as `dayofmonth`, ID, post_name, post_title FROM $wpdb->posts WHERE post_type = 'article' AND post_status = 'publish' ORDER BY post_date DESC" ; $key=m d5($query);
$cache=w p_cache_get( 'mp_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults=$ wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_add( 'mp_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ($arcresults) { $last_year = 0; $last_month = 0; foreach ( $arcresults as $arcresult ) { $year = $arcresult->year;
$month = $arcresult->month; if ($year != $last_year) { $last_year = $year; $last_month = 0; ?>
<tr class=year>
<th>
<br />
<br />
<?php echo $arcresult->year; ?></th>
</tr>
<?php } if ($month !=$ last_month) { $last_month=$ month; ?>
<tr class=archive>
<th>
<?php echo $wp_locale->get_month($arcresult->month); ?></th>
<td></td>
</tr>
<?php } ?>
<tr class=archive>
<th>
<?php echo $arcresult->dayofmonth; ?></th>
<td id=p<?php echo $arcresult->ID; ?>>
<a href="/<?php echo $arcresult->post_name; ?>">
<?php echo strip_tags(apply_filters( 'the_title', $arcresult->post_title)); ?></a>
</td>
</tr>
<?php } } ?>
</table>
</div>
<!-- /end .container -->
Maybe:
<table width="200" border="1">
<tbody>
<tr>
<td>
<div>2017</div>
<div>Januari</div>
<div>1</div>
</td>
<td>Long title here</td>
</tr>
<tr>
<td>
<div>2016</div>
<div>Januari</div>
<div>1</div>
</td>
<td>Long title here</td>
</tr>
</tbody>
</table>

Avoid empty lines in php checkbox

I have html form wherein 7 check box are there. When all the boxes are checked, it is giving good result. Whereas any body gives 3 or 4 check, it shows the result with empty lines in between. It looks very indecent. Any body can suggest the corrective code for that.
the HTML code for the check box:
CONDITIONS: <br />
<input type="checkbox" name="c1" value="Invoice">Invoice <br />
<input type="checkbox" name="c2" value="LR copy">LR copy <br />
<input type="checkbox" name="c3" value="Test Reports">Test Reports<br />
<input type="checkbox" name="c4" value="Draft"> Draft<br />
<input type="checkbox" name="c5" value="Check-list">Check-List <br />
<input type="text" name="c6" placeholder=" Add any" maxlength="25" size="40"><br />
<input type="text" name="c7" placeholder=" Add any clause" maxlength="25" size="40">
the php code for the above check-box:
<html>
<head>
<style type="text/css">
table { margin: 1em 0 0 0; border-collapse: collapse; }
td {margin: 5px 0 0 0;padding: .4em ; border: 0px solid; height: 12px; }
tr td:first-child {font-weight: bold; width: 250px; color: black; height: 10px; font-size: 18px;}
tr td:last-child {width: 450px; color: blue;height: 10px; font-size: 18px; }
p {margin: 5px 0 0 20px; width: 700px; line height: 20px; text-align: justify; font-size:105%;
font-family: Arial, "Lucida Sans Unicode", Verdana, sans-serif;}
#trim {margin: 5px 0 0 20px; width: 700px; line height: 20px; text-align: center; font-size:150%;
font-family: Arial, "Lucida Sans Unicode", Verdana, sans-serif;}
.z{ margin: 5px 0 0 20px; width: 700px;}
.z1{ margin: 5px 0 0 20px; width: 700px;}
</style>
</head>
<body>
<div id="trim">
<b> FORMAT </b>
<hr size="2" noshade>
</div>
<?
$k1 = strtoupper($_REQUEST['k1']);
$k2 = strtoupper($_REQUEST['k2']);
$k3 = strtoupper($_REQUEST['k3']);
$k4 = strtoupper($_REQUEST['k4']);
$k5 = strtoupper($_REQUEST['k5']);
$k6 = strtoupper($_REQUEST['k6']);
$k7 = strtoupper($_REQUEST['k7']);
$k22 = strtoupper($_REQUEST['k22']);
$k23 = strtoupper($_REQUEST['k23']);
$k8 = strtoupper($_REQUEST['k8']);
$k9 = strtoupper($_REQUEST['k9']);
$k10 = strtoupper($_REQUEST['k10']);
$k11 = strtoupper($_REQUEST['k11']);
$k12 = strtoupper($_REQUEST['k12']);
$k13 = strtoupper($_REQUEST['k13']);
$k14 = strtoupper($_REQUEST['k14']);
$k15 = strtoupper($_REQUEST['k15']);
$k16 = strtoupper($_REQUEST['k16']);
$c6 = strtoupper($_REQUEST['c6']);
$c7 = strtoupper($_REQUEST['c7']);
$k19 = strtoupper($_REQUEST['k19']);
$k20 = strtoupper($_REQUEST['k20']);
$k21 = strtoupper($_REQUEST['k21']);
$r1 = strtoupper($_REQUEST['r1']);
$r2 = strtoupper($_REQUEST['r2']);
$r3 = strtoupper($_REQUEST['r3']);
$r4 = strtoupper($_REQUEST['r4']);
$r5 = strtoupper($_REQUEST['r5']);
$r6 = strtoupper($_REQUEST['r6']);
$r7 = strtoupper($_REQUEST['r7']);
$r8 = strtoupper($_REQUEST['r8']);
$r9 = strtoupper($_REQUEST['r9']);
$r10 = strtoupper($_REQUEST['r10']);
$c1 = strtoupper($_REQUEST['c1']);
$c2 = strtoupper($_REQUEST['c2']);
$c3 = strtoupper($_REQUEST['c3']);
$c4 = strtoupper($_REQUEST['c4']);
$c5 = strtoupper($_REQUEST['c5']);
$c8 = strtoupper($_REQUEST['c8']);
$c9 = strtoupper($_REQUEST['c9']);
$c10 = strtoupper($_REQUEST['c10']);
$c11 = strtoupper($_REQUEST['c11']);
$c12 = strtoupper($_REQUEST['c12']);
$c13 = strtoupper($_REQUEST['c13']);
$c14 = strtoupper($_REQUEST['c14']);
$c15 = strtoupper($_REQUEST['c15']);
$c16 = strtoupper($_REQUEST['c16']);
$c17 = strtoupper($_REQUEST['c17']);
$c18 = strtoupper($_REQUEST['c18']);
$c19 = strtoupper($_REQUEST['c19']);
$c20 = strtoupper($_REQUEST['c20']);
$c21 = strtoupper($_REQUEST['c21']);
$c22 = strtoupper($_REQUEST['c22']);
$c23 = strtoupper($_REQUEST['c23']);
$c24 = strtoupper($_REQUEST['c24']);
$c25 = strtoupper($_REQUEST['c25']);
$p1= strtoupper($_REQUEST['p1']);
$p2 = strtoupper($_REQUEST['p2']);
if(empty($k2)){$kk=" "; }else{$kk="(Code: $k2)";}
$html = "<p> <div class='z1'>
<b><u> PROJECT DETAILS </u></b>
<table>
<tr><td> Project Name: </td> <td > $k1 $kk</td></tr>
<tr><td> Purchase Order no:</td> <td > $k3 Dtd: $k4</td></tr>
<tr><td>Total Order Value:</td> <td >$k6 </td></tr>
<tr><td> Materials Ordered </td> <td >$k5 </td></tr>
<tr><td> Supplier Details: </td> <td >$k22 <br /> $k23</td></tr>
</table>
<br /><b><u> DISPATCH CONDITIONS </u></b>
<table>
<tr><td> Shipment from: </td> <td > $k11</td></tr>
<tr><td> Shipment to: </td> <td >$k12 </td></tr>
<tr><td> Dispatch starting date: </td> <td >$k9 </td></tr>
<tr><td> Last date of dispatch: </td> <td >$k10 </td></tr>
<tr><td> Part-Shipment: </td> <td >$r1 </td></tr>
<tr><td> Part-Transshipment: </td> <td > $r2</td></tr>
<tr><td> Transport Charges: </td> <td > $r4</td></tr>
<tr><td> Insurance Charges: </td> <td > $r3</td></tr>
<tr><td> Basis of price: </td> <td > $r5</td></tr>
</table>
<br /><b><u> BANK DETAILS </u></b>
<table>
<tr><td> L/C amount: </td> <td > $k8 <br /> Rs. $k30 </td></tr>
<tr><td> Usance period: </td> <td > $r9 Days from the date of dispatch.</td></tr>
<tr><td> Usance interest: </td> <td>$k15 Days to Supplier A/c. and $k16 Days to Buyer A/c.</td></tr>
<tr><td> Usance interest charges: </td> <td >Not more than $k14 % p.a </td></tr>
<tr><td> L/C Charges: </td> <td > Opening charges to $r7 and discount charges to $r8</td></tr>
<tr><td> Negotiation to be done: </td> <td > within $r10 Days from the date of dispatch.</td></tr>
<tr><td> Place of Expiry: </td> <td > $k21</td></tr>
</table>
<br /><b><u> DOCUMENTS REQUIRED:</u></b>
<ul><li>$c1
</li><li>$c2
</li><li>$c3
</li><li>$c4
</li><li>$c5
</li><li>$c6
</li><li>$c7
</li></ul>
<br /><b><u> L/C CONDITIONS </u></b>
<ul><li>
$c8 </li><li>
$c9 </li><li>
$c10</li><li>
$c11 </li><li>
$c12</li><li>
$c13 </li><li>
$c22 </li><li>
$c23 </li></ul>
<br /><b><u> ADDITIONAL CONDITIONS </u></b>
<ul><li>
$c14 </li><li>
$c15 </li><li>
$c16 </li><li>
$c17 </li><li>
$c18 </li><li>
$c19 </li><li>
$c20 </li><li>
$c21 </li><li>
$c24 </li><li>
$c25 </li><li>
$k19 </li><li>
$k20 </li></ul>
</div></p>";
$html .= "<div class='z'> <hr /> Place: <b>".$p1."</b></div>";
$html .= "<div class='z'> Date: $p2 <div style='float:right;font-size: 12pt'><i> (Authorized Signature ) </i></div></div>";
print($html);
?>
</body>
</html>
You are always echoing all the lines. when some values are empty, there will be just empty line (list item). So you have to check that the values exists. Little example:
<?
$html = '<p>CONDITIONS:</p><ul>';
function echoRow(&$html, $val) {
if (isset($val) && !empty($val))
$html .= '<li>' . $val . '</li>';
}
echoRow($c1);
echoRow($c2);
echoRow($c3);
echoRow($c4);
echoRow($c5);
echoRow($c6);
echoRow($c7);
$html .= '</ul>';
print($html);
?>
This is because when a checkbox is not checked, then its value is not send to server. For example if C1 is not checked, then it is not send in $_POST values. What you can do is check if each of checkbox is set and display only those whose value exits. If you want you can show alternative message for checkbox that is not set. You can do something like this
$html="<ul>";
if(isset($c1))
$html.="<li>$c1</li>"
else
{
//do nothing or show alternative message
$html.="<li>Invoice not selected</li>"
}
// do same for $c2....$c7
$html.="</ul>";
print($html);

want to insert in attendence first time not update it

hi this is my code which updates an attendance system, but I dont know how to insert attendance for first time. Where should I use insert query? it also shows error and not updating the table. Somebody please guide me
<?php
$connection=mysql_connect("localhost","root","") ordie(mysql_error()
$db=mysql_select_db("Project") or die(mysql_error());
$sql = "SELECT Fac_name FROM Faculty ORDER BY Fac_name ASC ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<form name="Attendence" method="post" action="A.php">
<table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px">
<tbody>
<tr>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Faculty Name</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Abesent</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Present</th>
<th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Total present</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)) {
?>
<tr>
<td class="table1">
<? $id[] = $rows['Fac_name']; ?><? echo $rows['Fac_name'];?>
</td>
<td class="table1">
<input name="date[<? echo $rows['Fac_name']; ?>]" type="text" >
</td>
<td id="present">
<input type="radio" name="Present[<? echo $rows['Fac_name']; ?>]" checked="checked" >Present
</td>
<td id="absent">
<input type="radio" name="Absent[<? echo $rows['Fac_name']; ?>]" value="ABSENT">Absent
</td>
<td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;"></td>
</tr>
<?php }?>
<tr>
<td colspan="7" style="vertical-align:middle; text-align: center;">
<br><br>
<input id="Submit" type="submit" name="Submit" value="Insert" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;">
</td>
</tr>
</tbody>
</table>
</form>
<?php
if(isset($_POST['Submit'])) {
foreach($_POST['Present'] as $id => $value) {
$date=$_POST['date'];
$present=$_POST['Present'];
$absent=$_POST['Absent'];
$sql = "INSERT INTO Attendence(Fac_name, date, Present, Absent) VALUES ('".$id."', '$date[$value]', '$present[$value]', '$absent[$value]', '".$value."') ";
$result = mysql_query($sql);
}
}
if($result) {
//header("location:A.php");
} else {
//print_r ($_POST);
echo "Your entry is not completed at this time.............";
}
if(isset($_POST['submitattend'])) {
set_time_limit(0);
$class1 = $_SESSION['bra'];
$q3 = mysql_query("Select Id from `Faculty` order by `Id` ASC"); // get all roll numbers
$count = mysql_num_rows($q3);
$j = 1;
while($q4 = mysql_fetch_array($q3)) {
if(isset($_POST['chk'.$j])) {
$v2 = $q7['finalattend']+1; //total attendance of student
$v3 = $q7['totalattend']+1; //total attendance taken by teacher
mysql_query("UPDATE `Attendence` SET `finalattend`='".$v2."', `totalattend`='".$v3."' where `attenduser`='".$v1."'") or die(mysql_error());
} else {
$v2=$q7['totalattend']+1;
mysql_query("UPDATE `Attendence` SET `totalattend`='".$v2."' where `attenduser`='".$v1."'") or die(mysql_error());
}
$j=$j+1;
}
header("Location: 12.html"); //logout after taking attendance..
}
?>
$date_value = $date[$value];
$present_value = $present[$value];
$absent_value = $absent[$value];
$check_result = mysql_query("selct count(*) from Attendence where Fac_name = '$id' AND date = '$date_value' AND Present = '$present_value' AND Absent = '$absent_value' ");
if($check_result == 0)
{
// insert query
}
else
{
// update query
}
I think your INSERT query line has Syntax error Please correct it . You have only three row names - Fac_name, date, Present, Absent but are inserting 5 values. Besides that quotes are not used correctly

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".

Categories