Jump to content

how to saving php table to SQL


havide

Recommended Posts

I have problem how to saving all my record. 

9th5j8.png

 

when I save I just got 1 record saved in mySQL and it the last record on that table. Here is my code =


<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO thasil (nrp, nama, pangkat, mgolongan, mtes_fisik, mkemampuan, hasil) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['nrp'], "int"),
                       GetSQLValueString($_POST['nama'], "text"),
                       GetSQLValueString($_POST['pangkat'], "text"),
                       GetSQLValueString($_POST['mgolongan'], "int"),
                       GetSQLValueString($_POST['mtes_fisik'], "double"),
                       GetSQLValueString($_POST['mkemampuan'], "double"),
                       GetSQLValueString($_POST['hasil'], "double"));

  mysql_select_db($database_stok, $stok);
  $Result1 = mysql_query($insertSQL, $stok) or die(mysql_error());

  $insertGoTo = "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_stok, $stok);
$query_anggotaset = "SELECT * FROM tanggota ORDER BY nrp ASC";
$anggotaset = mysql_query($query_anggotaset, $stok) or die(mysql_error());
$row_anggotaset = mysql_fetch_assoc($anggotaset);
$totalRows_anggotaset = mysql_num_rows($anggotaset);

mysql_select_db($database_stok, $stok);
$query_bobotset = "SELECT * FROM tbobot";
$bobotset = mysql_query($query_bobotset, $stok) or die(mysql_error());
$row_bobotset = mysql_fetch_assoc($bobotset);
$totalRows_bobotset = mysql_num_rows($bobotset);

$crMax = mysql_query("SELECT min(Golongan) as minK1, 
max(tes_fisik) as maxK2,
max(kemampuan) as maxK3
FROM tanggota");
$max = mysql_fetch_array($crMax);

$no=1;



?>


<?php include ('header.php') ?>

<div id="conten-wrapper">
<div id="conten">
<h1> Normalisasi Anggota Kesamaptaan </h1>
<p> <a href="cetak.php"><img src="../images/btn_list.png" height="20"/> >>> Lihat & Cetak Hasil Akhir Perangkingan</a></p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table border="1" cellpadding="3" cellspacing="1">
  <tr>
  <td>no</td>
    <td>nrp</td>
    <td>nama</td>
    <td>pangkat</td>
    <td>C1 Golongan</td>
    <td>C2 tes_fisik</td>
    <td>C3 kemampuan</td>
     <td>hasil</td>
    
    </tr>
  <?php 
  
  do { 
  $hasil= round (($max['minK1']/$row_anggotaset['Golongan']*$row_bobotset['golongan']+
 $row_anggotaset['tes_fisik']/$max['maxK2']*$row_bobotset['tes_fisik']+
 $row_anggotaset['kemampuan']/$max['maxK3']*$row_bobotset['kemampuan']),2) ;
?>
  
    <tr>
    <td><?php echo $no; $no++;?></td>
      <td><input type="text" name="nrp" value="<?php echo $row_anggotaset['nrp']; ?>"  readonly/></td>
      <td><input type="text" name="nama" value="<?php echo $row_anggotaset['nama']; ?>"  readonly/></td>
       <td><input type="text" name="pangkat" value="<?php echo $row_anggotaset['pangkat']; ?>" size="4" readonly/></td>
      <td><input type="text" name="mgolongan" value="<?php echo round ($max['minK1']/$row_anggotaset['Golongan']
 *$row_bobotset['golongan'],2); ?>" size="2" readonly/></td>
      <td><input type="text" name="mtes_fisik" value="<?php echo round ($row_anggotaset['tes_fisik']/$max['maxK2']
 *$row_bobotset['tes_fisik'],2); ?>" size="2" readonly/></td>
       <td><input type="text" name="mkemampuan" value="<?php echo round ($row_anggotaset['kemampuan']/$max['maxK3']
  *$row_bobotset['kemampuan'],2); ?>" size="3" readonly/></td>
       <td><input type="text" name="hasil" value="<?php echo $hasil ?>" size="2" readonly/></td>
      </tr>
      
    <?php } while ($row_anggotaset = mysql_fetch_assoc($anggotaset));?>
    <tr valign="baseline">
      <td colspan="8" align="right" nowrap="nowrap"><input type="submit" value="Insert record" /></td>
      </tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
</div>


<?php include ('footer.php') ?>



</body>
</html>
<?php
mysql_free_result($anggotaset);

mysql_free_result($bobotset);
?>

Link to comment
Share on other sites

thanks for advice mac_gyver, i already read it before post it, actually i have no idea about how to implement to my code. I also got another sample from friend with using for(start,condition,amount) {statement;}. I think I need work hard to solve this. 

Link to comment
Share on other sites

 

Let's start with the basic idea. You need to name your variables in the html as an array

<input type="text" name="nrp[]">

then you loop through the array that you get and write the records:

foreach($_POST['nrp'], as $value){
WRITE YOUR RECORD HERE
}

 

I think its good and I will try it. Thx :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.