table not update

Alles, was PHP betrifft, kann hier besprochen werden.

table not update

Postby pkimran » 15. May 2015 13:29

Hy
I have two tables
1. table data column ItemID
2. table main column ItemID

enter the ItemId from 1 to 10 in table data.

in php form in select box fetch the ItemID from data.
but after select ItamId '1' or else and submit it does not save in table main column ItemID.
the code is below

Code: Select all
function print_Form()
 {
//to get ItemID from table data for form
 $ItemID=mysql_query("SELECT ItemID FROM data");
$option5 = '';
 while($row = mysql_fetch_assoc($ItemID))
{
  $option5 .= '<option value = "'.$row['ItemID'].'">'.$row['ItemID'].'</option>';
}



//Form
$newform = "<br><form method=\"post\"action=\"{$_SERVER['PHP_SELF']}\">
<table>
<td>
<label><b>Item ID:</b></label>
<select >
 echo .$option5;
</select>
</td>
<input  type=\"submit\"value=\"Add Record\">
</table>

return $newform;

}

//saving record
function save_Record() {
   $_GET["ItemID"]="";
$locationErr="";
$ItemID=  (int) cleanString  ($_GET['ItemID']);

//query
$query = "INSERT INTO main(ItemID)
VALUES ($ItemID)

//execute query
if(mysql_query ($query)) {
   print "<div '>
   Record added successfully </div>";
   print print_Form(); //show form again
}else{
   error_Message (mysql_error()); //if error in query
   
}
}



pkimran
 
Posts: 10
Joined: 03. April 2015 14:21
Operating System: windows 7

Re: table not update

Postby Nobbie » 15. May 2015 14:16

There are tons(!) of errors in that code, as well Syntax errors as logic errors. First of all get your code running (this code does not run), then ask again.

There is missing a closing quote, missing semicolon, there is no mysql_connect(), there is no call to any of the two functions - it is horrible bad code. There are at least no PHP start and end tags.
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: table not update

Postby pkimran » 16. May 2015 05:21

very sorry for this trouble. actually i did not send complete code cz might be it would difficult to read for you. now i am sending complete code. every thing is accurate but after submit, the select box item does not update in mysql like ItemID, Description,Unit, Designation, Department.


Code: Select all

<?php



//conenction file

include "connection.php";


//cleaning string
function cleanString ($string)
{
   $string=htmlspecialchars (trim($string));
   return $string;
}


//function for new form

function print_Form()
 {
   //get Description
 $Desc=mysql_query("SELECT Description FROM data");
$option1 = '';
 while($row = mysql_fetch_assoc($Desc))
{
  $option1 .= '<option value = "'.$row['Description'].'">'.$row['Description'].'</option>';
}
//get unit
 $Unit=mysql_query("SELECT Unit FROM data");
$option2 = '';
 while($row = mysql_fetch_assoc($Unit))
{
  $option2 .= '<option value = "'.$row['Unit'].'">'.$row['Unit'].'</option>';
}
//get Designation
 $Designation=mysql_query("SELECT Designation FROM data");
$option3 = '';
 while($row = mysql_fetch_assoc($Designation))
{
  $option3 .= '<option value = "'.$row['Designation'].'">'.$row['Designation'].'</option>';
}
//get department
 $Dept=mysql_query("SELECT Dept FROM data");
$option4 = '';
 while($row = mysql_fetch_assoc($Dept))
{
  $option4 .= '<option value = "'.$row['Dept'].'">'.$row['Dept'].'</option>';
}
//itemid
 $ItemID=mysql_query("SELECT ItemID FROM data");
$option5 = '';
 while($row = mysql_fetch_assoc($ItemID))
{
  $option5 .= '<option value = "'.$row['ItemID'].'">'.$row['ItemID'].'</option>';
}

   
$newform = "<br><form method=\"post\"action=\"{$_SERVER['PHP_SELF']}\">
   
   <fieldset id=body >

<table>



<tr >
<td>
<label><b>Item ID:</b></label>

<select >
 echo .$option5;
</select>
</td>

<td >
<label><b>Date:</b></label>

<input type=\"date\"name=\"Date\">
</td>





<td>
<label><b>Description:</b></label>

<select>
 echo .$option1;
</select>
</td>
</tr>



<tr>
<td>
<label><b> Unit:</b></label>

<select>
 echo .$option2;
</select>
</td>


<td>
<label><b>Debit:</b></label>
<input type=\"decimal\"name=\"Debit\"></td>




<td>
<label><b>Credit:</b></label>

<input type=\"decimal\"name=\"Credit\"></td>
</tr>



<tr>
<td>
<label><b> To: </b></label>

<input type=\"text\"name=\"Towhere\"></td>


</td>

<td>

<label><b>From:</b></label>
<input type=\"text\"name=\"Fromwhere\"></td>




<td>
<label><b> Designation: </b> </label>

<select>
 echo .$option3;
</select>
</td>
</tr>

<tr>
<td >
<label><b> Department: </b> </label>

<select>
 echo .$option4;
</select>
</td>






<td>
<label><b>Remarks:</b></label>
<input type=\"text\"name=\"Remarks\" ></td>




</table>


<input  type=\"submit\"value=\"Add Record\">
<input type=\"hidden\"value=\"true\"name=\"post\">
</fieldset>
</form>";


return $newform;

}



//saving record
function save_Record() {
   $_POST["ItemID"]="";
$locationErr="";

$_POST["Description"]="";
$locationErr="";

   $_POST["Unit"]="";
$locationErr="";

$_POST["Dept"]="";
$locationErr="";

$_POST["Designation"]="";
$locationErr="";


   $ItemID=  (int) cleanString  ($_POST['ItemID']);
$Date=   cleanString  ($_POST['Date']);
$Description= cleanString  ($_POST['Description']);
$Unit = (string)  ($_POST['Unit']);
$Debit = (int) cleanString ($_POST['Debit']);
$Credit =(int)  cleanString ($_POST ['Credit']);
$Towhere =  cleanString ($_POST ['Towhere']);
$Fromwhere =  cleanString ($_POST ['Fromwhere']);
$Designation =  cleanString ($_POST ['Designation']);
$Dept =  cleanString ($_POST ['Dept']);
$Remarks =  cleanString ($_POST ['Remarks']);





//checking for empty values

//query

$query = "INSERT INTO main(ItemID,Date,Description,Unit,Debit,Credit,Towhere,Fromwhere,Designation,Dept,Remarks)
VALUES
($ItemID,'$Date','$Description','$Unit',$Debit,$Credit,'$Towhere','$Fromwhere','$Designation','$Dept','$Remarks')";
   
//execute query
if(mysql_query ($query)) {
   print "<div style='background:#FFFFCC;border:dashed 1px #CCCCCC' id='inner' class='wrap'>
   Record added successfully </div>";
   print print_Form(); //show form again
}else{
   error_Message (mysql_error()); //if error in query
   
}
}

//error message
function error_Message ($error) {
   $msg = "<div style='background:#FFFFCC;border:dashed 1px #CCCCCC' id='inner' class='wrap'>;
   $error <a href='javascript:history.go (-1) '>Back</a></div>";
   die($msg);
}
//function for record listing
function show_Records_List() {
   //page name for link, this will same the page, require for pagging
   $page_name = $_SERVER ['PHP_SELF'];
   //get start value for page
   
   $start =(isset($_GET['start']));
   if (!isset($start)) {
      $start=0;
   }
   //records per page
   $limit = 5;
   //next and previous operation
   $back = $start - $limit;
   $next = $start + $limit;
   //retrive data from table
   $main2 = mysql_query ("SELECT * FROM main") or
   error_Message (mysql_error());
   
   //get number of records in table
   
   $nume = mysql_num_rows ($main2);
   $main = mysql_query ("SELECT * FROM main ORDER BY ItemID DESC LIMIT $limit") or
   error_Message (mysql_error());
   // if no record exist
   if (mysql_num_rows ($main) <=0) {
      error_Message ("No record in result table");
   }
   
   //table header

   print " <table id=inner  >
   
   <tr  bgcolor='#CCCCCC' >
   <th   >Item ID</th>
   <th >Date</th>
      <th >Description</th>
         
      <th >Unit</th>
         <th >Debit</th>
         <th >Credit</th>
      <th >To</th>
         <th >From</th>
         <th >Designation</th>
      <th >Department</th>
      <th >Remarks</th>
         <th >Edit </th>
      <th >Delete</th>
      </tr>
   ";   
   // loop through record set, create table and show results
   while ($Row = mysql_fetch_array ($main)) {
      print
      "<tr  >
      <td >" .$Row['ItemID']."</td>
      <td >" .$Row['Date']."</td>
      <td >" .$Row['Description'] . "</td>
      <td >" .$Row['Unit']."</td>
      <td >" .$Row['Debit'] . "</td>
      <td >" .$Row['Credit']."</td>
      <td >" .$Row['Towhere'] . "</td>
      <td >" .$Row['Fromwhere']."</td>
      <td >" .$Row['Designation'] . "</td>
      <td >" .$Row['Dept'] . "</td>
      <td >" .$Row['Remarks'] . "</td>
      <td ><a class='button' href='edit.php?do=edit&ItemID=" .$Row['ItemID']."'>Edit</a></td>
      <td > <a class='button' href='edit.php?do=del&ItemID=" .$Row['ItemID']."'>Delete</a></td></tr>";
   }
   print "</table>
   <br><a  class='button' href='main.php'>Get All Record</a>";
   
   print "\n<a class='button' href='all_search.php'>Search All</a>";
   print "\n<a class='button' href='Town_wise.php'>Report</a>";
   
   }

   
//what to do
if(isset($_POST
['post']) ){
   // if form is POSTED
   save_Record();
}else{
   //first time print the form
   print print_Form();
}

//Result listing
print "<h3  align='center'>
Recent Records</h3>";


show_Records_List();


?>

pkimran
 
Posts: 10
Joined: 03. April 2015 14:21
Operating System: windows 7

Re: table not update

Postby Nobbie » 16. May 2015 10:39

pkimran wrote:the select box item does not update in mysql like ItemID, Description,Unit, Designation, Department.


Of course not, you destroy all values of the $_POST array at the top of the save_Record function:


Code: Select all
//saving record
function save_Record() {
   $_POST["ItemID"]="";   // this destroys ItemID
$locationErr="";

$_POST["Description"]="";   // this destroys Description
$locationErr="";

   $_POST["Unit"]="";    // this destroys Unit
$locationErr="";

$_POST["Dept"]="";         // this destroys Dept
$locationErr="";

$_POST["Designation"]="";   // this destroys Designation
$locationErr="";


What a nonsense?!
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: table not update

Postby pkimran » 16. May 2015 10:43

yeah its a nonsense. sorry for that but i am not a professional webdeveloper :(
pkimran
 
Posts: 10
Joined: 03. April 2015 14:21
Operating System: windows 7

Re: table not update

Postby pkimran » 16. May 2015 10:47

now i remove the code that destroying all values but now there are some errors

Notice: Undefined index: ItemID in C:\xampp\htdocs\Store\index.php on line 252

Notice: Undefined index: Description in C:\xampp\htdocs\Store\index.php on line 254

Notice: Undefined index: Unit in C:\xampp\htdocs\Store\index.php on line 255

Notice: Undefined index: Designation in C:\xampp\htdocs\Store\index.php on line 260

Notice: Undefined index: Dept in C:\xampp\htdocs\Store\index.php on line 261


:(
pkimran
 
Posts: 10
Joined: 03. April 2015 14:21
Operating System: windows 7

Re: table not update

Postby Nobbie » 16. May 2015 11:04

Your select-Tags are missing a name attribute, therefore there is no corresponding field in the $_POST array. Example


Your code:

Code: Select all
<label><b>Description:</b></label>

<select>
 echo .$option1;
</select>


There is no name for the select, but you want to have the name "Description". HTML and PHP cannot know, what name you will need later on, therefore you have to specify one:

Code: Select all
<label><b>Description:</b></label>

<select name=\"Description\">
 echo .$option1;
</select>


This name is missing for all of your select tags.

See http://www.w3schools.com/tags/tag_select.asp for detailed documentation
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04

Re: table not update

Postby pkimran » 18. May 2015 05:54

thanks alot for help dear. :)
pkimran
 
Posts: 10
Joined: 03. April 2015 14:21
Operating System: windows 7

Re: table not update

Postby kuwar » 10. June 2015 10:40

Hi
<form method="post" action="klas_bewerken.php">
<label>Old</label> <input name="old" type="text" id="textfield" />
<label>New</label> <input name="new" type="text" id="textfield" />
<input type="submit" value="Change now" />
</form>

Klas_bewerken.php

<?php
$mysqli = new mysqli("localhost", "root", "root", "sms");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$old = $_POST['old'];
$new = $_POST['new'];

echo "Old: $old";
echo "<br /> New: $new";

mysqli_query("UPDATE klas SET klas_id='$new' WHERE klas_id='$old'");

ECHO "<br /><br />Updated.";

mysql_close($con);

?>
kuwar
 
Posts: 1
Joined: 10. June 2015 10:33
Location: Ludhiana
Operating System: Ubuntu

Re: table not update

Postby Nobbie » 10. June 2015 11:28

You are mixing mysqli class with mysqli function and also with mysql functions - that does not work:

Code: Select all
<?php
$mysqli = new mysqli("localhost", "root", "root", "sms");   // THIS is Mysqli Class
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$old = $_POST['old'];
$new = $_POST['new'];

echo "Old: $old";
echo "<br /> New: $new";

mysqli_query("UPDATE klas SET klas_id='$new' WHERE klas_id='$old'");  // ... but this is mysqli function, but missing connection handle, cannot work properly

ECHO "<br /><br />Updated.";

mysql_close($con);     // ... and finally this is mysql function and $con is an unknown variable

?>


Replace all calls to mysqli / mysql functions by a mysqli_ method (like mysqli->query for example). FInally add an error handling for mysqli->query (actually you do not evaluate, if the query was successfull or not).
Nobbie
 
Posts: 13165
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 47 guests