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>
3
  • What error says ? Add die(mysql_error ()); after your update query. Commented May 23, 2013 at 13:40
  • Instead of using $data[pid] you need to quote the key like $data['pid'] for both when you set it, and retrieve it Commented May 23, 2013 at 13:40
  • Hi, my codes run no error, but it not updating to db Commented May 23, 2013 at 13:59

3 Answers 3

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]

Sign up to request clarification or add additional context in comments.

2 Comments

It is indeed better (clearer), but he does not need to do it. PHP would expand it anyway.
@Iserni, if you are referencing the array key quoting, please read the "Do's and Don'ts" section on the docs php.net/manual/en/language.types.array.php. If you mean the curly braces in string interpolation read php.net/manual/en/… as arrays with quoted string keys will only work with {}
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());
}

5 Comments

I added an example of HERE-document (here called QUERY1), it allows for easier inclusion of text.
what my codes should look like? i'm very new, and clearly not understand your explanation. sorry before
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 php.net/manual/en/function.error-reporting.php ).
@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
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.
0

Try this change update query like this

mysql_query("update trainklasifier set datapos='".$inputpos."',datanet='".$inputnet."',dataneg='".$inputneg."' where pid='".$data[pid]."' ");

3 Comments

@fullybaked hi, i just change the code like above, but it not update the db
mysql_query("update trainklasifier set datapos='".$inputpos."',datanet='".$inputnet."',dataneg='".$inputneg."' where pid='".$data['pid']."'");
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.