I want to submit this form into DB php - php

i want to insert this below given form data to database, but i dont know how to do this, it is about inserting product to multiple warehouse with different quantities, when checkbox against the warehouse name is checked than the quantity textbox appears.
and this is my PHP code
<form name="form1" method="post" action="product_insert.php" enctype="multipart/form-data">
<table>
<tr>
<td width="274" align="right" height="25"><strong>Product Name :</strong></td>
<td><input type="text" name="wproname" value="" /></td>
</tr>
<tr>
<td width="274" align="right" height="25"><strong>Select Warehouse :</strong></td>
<td width="500"><table style="border:none">
<tr >
<td> Select </td>
<td> Name </td>
<td> Quantity </td>
</tr>
<?php
$sql="select * from tbl_warehouse where w_flag='1'";
$result=ExecuteGetRows($sql);
$num_rows=count($result); ?>
<?php for($i=0;$i<$num_rows;$i++){ ?>
<tr>
<td><input type="checkbox" name="chk<?php echo $result[$i]['w_id'];?>" value="<?php echo $result[$i]['w_id'];?>" id="chk<?php echo $result[$i]['w_id'];?>" onChange="display<?php echo $result[$i]['w_id'];?>();" />
</td>
<td><?php echo $result[$i]['w_name'];?></td>
<td><input type="text" name="qty<?php echo $result[$i]['w_id'];?>" id="qty<?php echo $result[$i]['w_id'];?>" style="display:none" />
</td>
</tr>
<script>
function display<?php echo $result[$i]['w_id'];?>()
{
if(document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="block";
}
if(!document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="none";
}
}
</script>
<?php } ?>
</table></td>
</tr>
<tr>
<td align="right"></td>
<td width="296"><input type="submit" name="Submit" value="Add New" align="middle" class="button login_btn"/></td>
</tr>
</table>
</form>
And I am using this php code for inserting data to the database
$sqls=mysql_query("select * from tbl_warehouse");
while($result=mysql_fetch_array($sqls)) {
$w = $result['w_id'];
echo $_POST['chk'.$w];
foreach($_POST['chk'.$w] as $key=>$val) {
echo $_POST['chk'.$w];
$sql = "INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
mysql_query($sql);
}
}

You don't need the leading comma in your values list in your insert statement. So change this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
To this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('".$wid."','".$wproname."','".$qty."')";

Related

Display dynamic table of checkbox in 3 columns in php

I have a database table of subjects. I have a web page that displays the list of subjects in 3 columns so that a user can select subjects using a checkbox. The challenge I have is it takes a long time for the list to display. The code is below:
<?php
$subjects = mysql_query("select id, name from subjects WHERE exam_type_id='0' OR exam_type_id='$exam_type_id' ORDER BY name");
$noOfSubjects = mysql_num_rows($subjects);
?>
<form name="form1" method="post" action="" class="FormCss" onsubmit=" return window.confirm('You are about to submit selected subjects. If you are not sure, cancel');">
<table>
<tr class="FormLabel2">
<td align="left" colspan="3" valign="top"></td>
</tr>
<?php
$i = 1;
while ($subject = mysql_fetch_array($subjects)){
// if it is a value that is supposed to be in the 1st cell of a row, print tr
if ($i==1 || $i==4 || $i==7 || $i== 10){
?>
<tr>
<?php
}
// print a td if value is not suppose to be the last on a row
if ($i%3 !=0){
?>
<td><input type="checkbox" name="subject[]"
value="<?php echo $subject['id']?>" <?php if (isCompulsoryMockSubject($subject['id'])){?> onclick="return false;" onkeydown="return false;" <?php }else{}?> <?php if (isCompulsoryMockSubject($subject['id'])){?> CHECKED="checked" <?php }else{}?> <?php if (hasSelectedSubject($_SESSION['candidate_id'], $exam_type_id, $subject['id'])){?> checked="checked" <?php }?>><?php echo $subject['name'];?>
</td>
<?php
}else{
// end the row
?>
<td><input type="checkbox" name="subject[]"
value="<?php echo $subject['id']?>" <?php if (isCompulsoryMockSubject($subject['id'])){?> onclick="return false;" onkeydown="return false;" <?php }?> <?php if (isCompulsoryMockSubject($subject['id'])){?> CHECKED="checked" <?php }else{}?> <?php if (hasSelectedSubject($_SESSION['candidate_id'], $exam_type_id, $subject['id'])){?> checked="checked" <?php }?>><?php echo $subject['name'];?>
</td>
</tr>
<?php
}
$i++;
}
?>
<tr>
<td width="34%" align="left" valign="top" class="FormLabel2"> </td>
<td width="33%" align="left" valign="top"><span
class="FormLabel2"> <input name="Submit" type="submit"
class="loginInputSubmit" value="Save"> </span></td>
<td width="33%" align="left" valign="top"> </td>
</tr>
<tr>
<td align="left" valign="top" class="FormLabel2"> </td>
<td colspan="2" align="right" valign="top" class="FormLabel2"> </td>
</tr>
</table>
</form>

unable to edit data in table using PHP form

Please look into this code unable to find out the actual error:
This is PHP upload code:
<?php
include_once("config.php");
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
if(isset($_POST['submitBtn'])) {
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
/*$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
} }
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
?>
This the form Part where data retrieve from table and when click on the update button nothing happened and page is redirected to view data page and showing the old data:
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
</div>
Form method is POST and you are using GET method in if loop
if(isset($_GET['pro_id']))
Use POST here.
I have made the changes in you complete code. Use this code and if required made the changes (if issues arrived)
PHP code
<?php
include_once("config.php");
if(isset($_POST['submitBtn']))
{
$dept_id = $_POST['dept_id'];
$cat_id = $_POST['cat_id'];
$pro_id = $_POST['pro_id'];*/
$pro_name = $_POST['pro_name'];
$pro_desc = $_POST['pro_desc'];
$pro_spec = $_POST['pro_spec'];
$pro_price = $_POST['pro_price'];
$status = 'on';
$pro_keywords = $_POST['pro_keywords'];
//image names
$pro_image = $_FILES['pro_image']['name'];
//temp images names
$temp_image = $_FILES['pro_image']['tmp_name'];
if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='')
{
echo "<script>alert('All the fields are mandatory')</script>";
exit();
}
else
{
//upload image to folder
move_uploaded_file($temp_image,"images/product_images/$pro_image");
$run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");
if($run_query1)
{
echo "<script>alert('Product updated successfully')</script>";
exit();
}
else
{
echo "<script>alert('Errors')</script>";
}
}
}
$query2 = array();
if(isset($_GET['pro_id']))
{
$id=$_GET['pro_id'];
$query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
$query2 = mysqli_fetch_array($query1);
}
?>
HTML Code
<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="650" border="0">
<tr>
<td width="183" align="right">Department:</td>
<th width="231" align="left">
<select name="dept_id" id="dept_id">
<option>Select Department</option>
<?php
$result=dept_show();
while($row=mysqli_fetch_assoc($result))
{
echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="183" align="right">Catagory</td>
<th width="231" align="left">
<select name="cat_id" id="cat_id">
<option>Select Catagory</option>
<?php
$result1=cat_show();
while($row=mysqli_fetch_assoc($result1))
{
echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
}
?>
</select></th></tr>
<tr>
<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>
</tr>
<tr>
<td align="right">Product Name/Model:</td>
<td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Description:</td>
<td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
</tr>
<tr>
<td align="right">Products Specification:</td>
<td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
</tr>
<tr>
<td align="right">Product Price:</td>
<td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
</tr>
<tr>
<td align="right">Product Image:</td>
<td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td></td>
<td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
</tr>
<tr>
<td align="right">Keywords:</td>
<td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
</tr>
</table>
</form>
</div> <?php } ?>
</td>
</tr>
</table>
Your pro_id field is commented out in your HTML using <!-- and -->, so the following never is true:
if(isset($_GET['pro_id']))
Also, you have a mismatch between your form method POST and $_GET that you are looking for.
Your query of update is correct? I think you must use
UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values
WHERE pro_id='$id'
And verify if your dept_id is INT as well cat_id, so if they are INT you don't need ''
UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id
Try to do this steps:
First thing comment out this line,
<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
next step,
you are sending data using POST i.e. (form method="post"), so use this
if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id'];
you will get $pro_id value.

PHP submit buttons will not work independently

New comer, I have 4 submit buttons as follow: home, log out, insert records for user and insert records for building. But if I press any of the four, it will insert a row in both add user and add building. I wish somebody with more experience than I can help and show me how to have my buttons work independently. Also I may have some bad habits please over look them or common.
<?php
$connect=mysql_connect("localhost","root","");
mysql_select_db("crud",$connect);
if(isset($_POST["insert_2"])) {
if($_POST["insert_2"]=="yes_2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="insert into Facility(Building) values('$Building')";
if(mysql_query($query2))
echo "<center>Record Inserted!</center><br>";
}
}
if(isset($_POST["update2"])){
if($_POST["update2"]=="yes2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="update Facility set Building='$Building' where id2=".$_POST['id2'];
if(mysql_query($query2))
echo "<center>Record Updated</center><br>";
}
}
if(isset($_GET['operation2'])){
if($_GET['operation2']=="delete"){
$query2="delete from Facility where id2=".$_GET['id2'];
if(mysql_query($query2))
echo "<center>Record Deleted!</center><br>";
}
}
//======================================================
if(isset($_POST["insert"])){
if($_POST["insert"]=="yes"){
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$username=$_POST["username"];
$password=$_POST["password"];
$usertype=$_POST["usertype"];
$query="insert into user(firstname, lastname, username, password,usertype) values( '$firstname', '$lastname','$username', MD5('$password'),'$usertype')";
if(mysql_query($query))
echo "<center>Record Inserted!</center><br>";
}
}
if(isset($_POST["update"])){
if($_POST["update"]=="yes"){
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$username=$_POST["username"];
$password=$_POST["password"];
$usertype=$_POST["usertype"];
$query="update user set firstname='$firstname' , lastname='$lastname' , username='$username' , password=MD5('$password'), usertype='$usertype' where id=".$_POST['id'];
if(mysql_query($query))
echo "<center>Record Updated</center><br>";
}
}
if(isset($_GET['operation'])){
if($_GET['operation']=="delete"){
$query="delete from user where id=".$_GET['id'];
if(mysql_query($query))
echo "<center>Record Deleted!</center><br>";
}
}
//------------------------------------------------------------------------------------------------
//if(isset($_POST['button1'])){
//}
//---------------------------------------------------------------------------------------------------------------
//if(isset($_POST['button2'])){
//}
//----------------------------------------------------------------------
?>
//-------------------Code removed----------------------------------------
<table align="center" border="0">
<tbody><tr>
<td> <font color="#000000">First Name:</font></td>
<td><input name="firstname" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Last Name:</font></td>
<td><input name="lastname" type="text"></td>
</tr>
<tr>
<td><font color="#000000">User Name:</font></td>
<td><input name="username" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Password:</font></td>
<td><input name="password" type="password"></td>
</tr>
<tr>
</tr><tr>
<td><font color="#000000">User Type:</font></td>
<td><input name="usertype" type="usertype"></td>
<td><font color="#000000">(1 for User or 2 for User Admin)</font></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input name="insert" value="yes" type="hidden">
<input value="Insert Record" type="submit">
</td>
</tr>
</tbody></table>
<?php if(isset($_GET['operation'])){
if($_GET['operation']=="edit"){
?>
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tbody>
<tr>
<td> <font color="#000000">First Name:</font></td>
<td><input name="firstname" value="<?php echo $_GET['firstname']; ?>" type="text"></td>
</tr>
<tr>
</tr>
<tr>
<td><font color="#000000">Last Name:</font></td>
<td><input name="lastname" value="<?php echo $_GET['lastname']; ?>" type="text"></td>
</tr>
<tr>
</tr><tr>
<td><font color="#000000">User Name:</font></td>
<td><input name="username" value="<?php echo $_GET['username']; ?>" type="text"></td>
</tr>
<tr>
<td><font color="#000000">Password:</font></td>
<td><input name="password" value="<?php echo $_GET['password']; ?>" type="text"></td>
</tr>
<tr>
<td><font color="#000000">User Type:</font></td>
<td><input name="usertype" value="<?php echo $_GET['usertype']; ?>" type="text"></td>
<td><font color="#000000">(1 for User or 2 for User Admin)</font></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input name="id" value="<?php echo $_GET['id'] ?>" type="hidden">
<input name="update" value="yes" type="hidden">
<input value="update Record" type="submit">
</td>
</tr>
</tbody></table>
</form>
<?php }}
?>
<?php $query="select * from user";
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>firstname</th>";
echo "<th>lastname</th>";
echo "<th>Username</th>";
echo "<th>Password</th>";
echo "<th>usertype</th>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['usertype']."</td>";
echo "<td><a href='ADPage.php?operation=edit&id=".$row['id'] ."&firstname=".$row['firstname']."&lastname=".$row['lastname']."&username=".$row['username']."&password=".$row['password']."&usertype=".$row['usertype']."'>edit</a></td>";
echo "<td><a href='ADPage.php?operation=delete&id=".$row['id']."'>delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<center>No Records Found!</center>";
}
?>
</td></tr></tbody></table><br><table style="width: 1157px; height: 507px; text-align: left; margin-left: auto; margin-right: auto;" border="4" cellpadding="2" cellspacing="2"><caption><big><font style="font-weight: bold;" size="+2"><big><span style="color: white;">Add and Remove Buildings</span></big></font></big></caption><tbody><tr><th style="white-space: nowrap; background-color: white;">
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tr>
<td>Building Name:</td>
<td><input type="text" name="Building" /></td>
</tr>
<td> </td>
<td align="right">
<input type="hidden" name="insert_2" value="yes_2" />
<input type="submit" value="Insert Record_2"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_GET['operation2'])){
if($_GET['operation2']=="edit"){
?>
<form method="post" action="ADPage.php">
<table align="center" border="0">
<tr>
<td>Building Name:</td>
<td><input type="text" name="Building" value="<?php echo $_GET ['Building']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td align="right">
<input type="hidden" name="id2" value="<?php echo $_GET['id2'] ?>" />
<input type="hidden" name="update2" value="yes2" />
<input type="submit" value="update Record"/>
</td>
</tr>
</table>
</form>
<?php
}}
?>
<?php
$query2="select * from Facility";
$result2=mysql_query($query2);
if(mysql_num_rows($result2)>0){
echo "<table align='center' border='1'>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Building Name</th>";
echo "</tr>";
while($row2=mysql_fetch_array($result2)){
echo "<tr>";
echo "<td>".$row2['id2']."</td>";
echo "<td>".$row2['Building']."</td>";
echo "<td><a href='ADPage.php?operation2=edit&id2=".$row2['id2'] ."&Building=".$row2['Building']."'>edit</a></td>";
echo "<td><a href='ADPage.php?operation2=delete&id2=".$row2['id2']."'>delete</a></td>";
echo "</tr>";
}
echo "</table>";
} else{
echo "<center>No Records Found!</center>";
} ;
?>
</th></tr></tbody></table></td>
</tr>
</tbody>
</table>
</center>
</form></td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
</center>
</body></html>
Do this:
<form method="POST">
<input name="button1" type="submit" value="Button 1 Label"/>
<input name="button2" type="submit" value="Button 2 Label"/>
<input name="button3" type="submit" value="Button 3 Label"/>
<input name="button4" type="submit" value="Button 4 Label"/>
</form>
and then:
// Do we have $_POST data?
if(!empty($_POST)){
var_dump($_POST); // to debug
if(!empty($_POST['button1'])){
// user clicked button 1
}elseif(!empty($_POST['button2'])){
// user clicked button 2
}elseif(!empty($_POST['button3'])){
// user clicked button 3
}elseif(!empty($_POST['button4'])){
// user clicked button 4
}
}
I think you can figure out how to apply this to your code. The submit buttons are identified by their names. If the 'name' is present in $_POST, you know that button was clicked. Independent of label (value).
Knowing which submit button was clicked, do what you need with the data in $_POST.
PS: Those hidden fields you seem to use to track the clicked button, they don't work like that.
PPS: Obviously, I didn't read that code too carefully. It's terrible! I just guessed your problem. Might be wrong...
Answer to your second question.. You do not need code like this:
$firstname; // Does nothing
$lastname; // Does nothing
$username; // Does nothing
$password; // Does nothing
$usertype; // Does nothing
$Building; // Does nothing
$connect; // Does nothing
global $firstname; // Not needed
global $lastname; // Not needed
global $username; // Not needed
global $usertype; // Not needed
global $row2; // Not needed
global $row; // Not needed
global $result2; // Not needed
global $result; // Not needed
if you want to initialize your variables you shoud just do it like:
$firstname = "";
in your code you have
<input type="hidden" name="insert_2" value="yes_2" />
and your php code says :
if(isset($_POST["insert_2"])) {
if($_POST["insert_2"]=="yes_2"){
$Building=$_POST["Building"];
$Building = Str_replace(" ","_",ltrim(rtrim($Building)) );
$query2="insert into Facility(Building) values('$Building')";
if(mysql_query($query2))
echo "<center>Record Inserted!</center><br>";
}
}
the hidden fields are always set at "yes_2" regardless. that's why your script is adding them .
Also keep in mind that
if(isset($_POST["update2"]))
will be true even if the value is nothing, try using
if(!empty($_POST["update2"]))
I didn't look through the whole code for obvious reasons lol. Does this help?

populate triple drop down in ajax and php in a form

I have managed to populate my three drop down lists in php and ajax. Here is the one part of my code that displays the three dropdown lists:
<?php
echo "<font id=\"categoria\"><select>\n";
echo "<option value='0'>Select the Firm</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria\"><select>\n";
echo "<option value='0'>Select the Claims Hub</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria2\"><select>\n";
echo "<option value='0'>Select the Area</option> \n" ;
echo "</select></font>\n";
?>
My question is: I also have a form on this pages and i want to make the three dropdown lists part of my form in order to post the values into another mysql table. Any suggestions how i can do this? Here is my form part:
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Select the Matter Type: </td>
<td><?php echo $dropdown; ?></td>
</tr>
<tr><td> </td>
<tr>
<td style="text-align: left; ">Active </td>
<td align="left" >
<input type="checkbox" name="active" value="1" />
</td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>
</table>
Why not just insert them into the html?
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Select the Matter Type: </td>
<td><?php echo $dropdown; ?></td>
</tr>
<tr>
<td>
<?php echo "<font id=\"categoria\"><select><option value='0'>Select the Firm</option></select></font>";?>
</td>
</tr>
<tr><td> </td>
<tr>
<td style="text-align: left; ">Active </td>
<td align="left" >
<input type="checkbox" name="active" value="1" />
</td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>

Updating data in table PHP mysql error

I am trying to update a data in a table which has 5 fields.
The fields are the following: proj_name, cust_name, address, cost and details.
Here is my code for the update (update_orde.php):
<?php
include("connect.php");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM project WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_orde_suc.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td colspan="5"><strong>Update Data</strong> </td>
</tr>
<tr>
<td align="center"><strong>Project Name</strong></td>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Cost</strong></td>
<td align="center"><strong>Details</strong></td>
</tr>
<tr>
<td><input name="pn" type="text" id="pn" value="<? echo $rows['proj_name']; ?>"></td>
<td align="center"><input name="cn" type="text" id="cn" value="<? echo $rows['cust_name']; ?>" size="15"></td>
<td align="center"><input name="add" type="text" id="add" value="<? echo $rows['address']; ?>" size="15"></td>
<td><input name="cost" type="text" id="cost" value="<? echo $rows['cost']; ?>" size="15"></td>
<td><input name="details" type="text" id="details" value="<? echo $rows['details']; ?>" size="15"></td>
</tr>
<tr>
<td align="center" colspan="5"><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> <input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// close connection
mysql_close();
?>
and here's the successful insertion into the database (update_orde_suc.php):
<?php
include("connect.php");
$id=$_POST['id'];
$pn = $_POST['proj_name'];
$cn = $_POST['cust_name'];
$add = $_POST['address'];
$cost = $_POST['cost'];
$det = $_POST['details'];
// update data in mysql database
$sql="UPDATE project SET proj_name='$pn', cust_name='$cn',address='$add', cost='$cost', details='$det' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='orders_edit.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
The problem is that when I try to change the data there is an error that says the 3 first indexes of update_orde_suc.php are undefined (cost and details are ok).
The weirdest thing is that I used the exact same code for another table update and it worked just fine, the only thing i did now was to change the names of the variables to correspond to the names of the new table.
You mixed up the variables badly. For example you save the proj_name coming from input as $pn and later when you are inserting it in the database you use $proj_name. You have to stick with the variable names throughout the code to work.

Categories