0

I'm working on updating text to sql. I am facing problem in my code below not updating the db, but when i change the where pid='$data[pid]' statement to some index like where pid='3', it works.

I'm new in programming. can you explain why? thanks.

<?php
//include "koneksi.php";
$host="localhost";//nama server
$user="root";//username 
$pass="";//password
$dbnama="skripsi";//nama database yang dipilih
mysql_connect($host, $user, $pass) or die ("Database tidak dapat di akses");//koneksi  ke database
mysql_select_db($dbnama); //database yang dipilih
?>
<?php
$query="select pid, username, user, datapos, datanet, dataneg, status from trainklasifier";
$hasil=mysql_query($query);
?>
<html>
<head>
</head>
<body>
<table border="1" align="center">
<tr>
  <td >ID</td>
  <td >Model Klasifikasi</td>
  <td >Creator</td>
  <td width="300px">Data Positif</td>
  <td width="300px">Data Netral</td>
  <td width="300px">Data Negatif</td> 
  <td width="100px">Status</td> 
  <td width="100px">Aksi</td> 
</tr>
<?php 
 $datapos=$data[datapos];
 $datanet=$data[datanet];
 $dataneg=$data[dataneg];
 $dataid=$data[pid];

 while ($data=mysql_fetch_array($hasil)){
     echo ("<tr><form id='form1' action='' method='post'> 
<td><textarea rows='1' cols='1' name='taid' value='$dataid' disabled>$data[pid]</textarea></td>    
<td>$data[username]</td>
<td>$data[user]</td>
<td><textarea rows='4' cols='35' name='tapos' >$data[datapos]</textarea></td>
<td><textarea rows='4' cols='35' name='tanet' value='$datanet'>$data[datanet]</textarea></td>
<td><textarea rows='4' cols='35' name='taneg' value='$dataneg'>$data[dataneg]</textarea></td>
<td>$data[status]</td>
<td><input type='submit' name='btsubmit' value='Train' /></td>
     </form></tr>");}
?>
<?php
$inputpos=$_POST['tapos'];
$inputnet=$_POST['tanet'];
$inputneg=$_POST['taneg'];
$id=$_POST['taid'];

if (isset($_POST['btsubmit'])){
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("skripsi") or die(mysql_error());
    mysql_query("update trainklasifier set datapos='$inputpos',datanet='$inputnet',dataneg='$inputneg' where pid='$data[pid]'"); 
    }
     echo $inputpos;
     ?>
     </table>
     </body>
     </html>
Tony Stark
  • 8,064
  • 8
  • 44
  • 63

3 Answers3

2

If you use arrays inside string interpolation you need to wrap then in {}

For example

"where pid = '{$data['pid']}'"

Also, it appears you are not quoting your array keys. $data[key] should be either $data["key"] or $data['key'] unless you are using a variable, as in $data[$key]

fullybaked
  • 4,117
  • 1
  • 24
  • 37
  • It is indeed better (clearer), but he does not *need* to do it. PHP would expand it anyway. – LSerni May 23 '13 at 13:52
  • @Iserni, if you are referencing the array key quoting, please read the "Do's and Don'ts" section on the docs http://php.net/manual/en/language.types.array.php. If you mean the curly braces in string interpolation read http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple as arrays with quoted string keys will only work with `{}` – fullybaked May 23 '13 at 14:14
1

I feel that $data['pid'] may be wrong. Consider:

$datapos=$data[datapos];
$datanet=$data[datanet];
$dataneg=$data[dataneg];
$dataid=$data[pid];

...here $data is okay, I assume...

while ($data=mysql_fetch_array($hasil)){

...here you cycle $data, and so exit when $data is NULL...

<td><input type='submit' name='btsubmit' value='Train' /></td>
 </form></tr>");}
                ^---

Here you have closed the cycle (I'd use a HERE-document if I were you, BTW), and so from now on $data is NULL.

$id=$_POST['taid'];

Here you have retrieved $id.

if (isset($_POST['btsubmit'])){
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("skripsi") or die(mysql_error());
    mysql_query("update trainklasifier set     datapos='$inputpos',datanet='$inputnet',dataneg='$inputneg' where pid='$data[pid]'");

And here you use $data[pid] which does not exist. The syntax would actually work, it is not too clear ({$data['pid']} would be better), but the problem is that $data is no longer an array here.

You probably want to use $id instead:

$query = <<<QUERY1
update trainklasifier
    set datapos='$inputpos',datanet='$inputnet',dataneg='$inputneg'
    where pid='$id';
QUERY1;
mysql_query($query);

Checking _POST

The POST checking code, if it is in the same file, ought to be enclosed in a suitable check that a POST was indeed originated:

<?php
    $need = array('tapos','tanet','taneg','taid','btsubmit');
    $haveAll = true;
    foreach($need as $fld)
        if (!isset($_POST[$fld]))
            $haveAll = false;
    if ($haveAll) {
        // Now we can proceed with POST.
        $inputpos=$_POST['tapos'];
        $inputnet=$_POST['tanet'];
        $inputneg=$_POST['taneg'];
        $id=$_POST['taid'];

        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("skripsi") or die(mysql_error());

// This is a here-document. Things to check: no two identifiers in the
// same PHP file (use QUERY1, QUERY2, ...). No spaces around the first
// opening tag ("<<<QUERY"). The closing tag and the semicolon must be
// the only thing on the closing line, no spaces, nothing: "QUERY1;"
// (These conditions are more restrictive than necessary: to be safe).

        $query = <<<QUERY1
update trainklasifier
    set datapos='$inputpos',datanet='$inputnet',dataneg='$inputneg'
    where pid='$id';
QUERY1;
        mysql_query($query) or die(mysql_error());
}
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • I added an example of HERE-document (here called QUERY1), it allows for easier inclusion of text. – LSerni May 23 '13 at 13:55
  • what my codes should look like? i'm very new, and clearly not understand your explanation. sorry before – user2413763 May 23 '13 at 14:01
  • I've added an example. There are several issues with the code, though: you may want to beef up on PDO DB and prepared queries, and your seem to check and assign from `$_POST` even when it seems not to be used. I strongly suggest you to turn error reporting to maximum during development (see http://php.net/manual/en/function.error-reporting.php ). – LSerni May 23 '13 at 14:07
  • @iserni i have turn on all msg error. and this is warning i get in my codes : Notice: Undefined index: tapos in C:\xampp\htdocs\labs\admin\index.php on line 49 Notice: Undefined index: tanet in C:\xampp\htdocs\labs\admin\index.php on line 50 Notice: Undefined index: taneg in C:\xampp\htdocs\labs\admin\index.php on line 51 Notice: Undefined index: taid in C:\xampp\htdocs\labs\admin\index.php on line 52 – user2413763 May 23 '13 at 14:13
  • Yes, you try reading from _POST when it is not filled. I've added a possible workaround. Often, you will want to have the different pieces of code in two separate PHP files, for better maintenance. – LSerni May 23 '13 at 14:39
0

Try this change update query like this

mysql_query("update trainklasifier set datapos='".$inputpos."',datanet='".$inputnet."',dataneg='".$inputneg."' where pid='".$data[pid]."' ");
fullybaked
  • 4,117
  • 1
  • 24
  • 37
Ashish Jain
  • 760
  • 1
  • 8
  • 23
  • @fullybaked hi, i just change the code like above, but it not update the db – user2413763 May 23 '13 at 13:47
  • mysql_query("update trainklasifier set datapos='".$inputpos."',datanet='".$inputnet."',dataneg='".$inputneg."' where pid='".$data['pid']."'"); – Ashish Jain May 23 '13 at 14:01
  • Hi, have you try with your editor? i have try all the answer given by the people, but it still not update to db. certainly your code above – user2413763 May 23 '13 at 14:07