i have question why i cannot query or fetch array my database content from variabel id in my database table? everytime i have sql command in my php script :

$query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'",$koneksi);
while($baris=mysql_fetch_array($query))

variable id='$id' always cannot be take out from database..
because variable id as the primary key, so the result is all of content in my database table cannot be shown in my localhost site..

Please help me, i don't know what to do...

Recommended Answers

All 8 Replies

How are you getting the id,i mean through Get,post or Session method?So are u sure the id is present?
Also i think it should be something like this $query = mysql_query("SELECT * FROM pengunjung WHERE id='$id',$koneksi"); though i dont know for what are u using $koneksi.

please remoe that ,$koneksi and execute the query.i think u can get the result.

if u are using $koneksi for nothing,then it should be something like this
1.get the id by either GET,POST OR SESSION
2.Then connect and select database. u can use include config file
3.

$query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'");
while($baris=mysql_fetch_array($query)){
$name=$baris['name'];//retrieve the information
$email=$baris['email'];
echo"$name<br/>";//display thei information
echo"$email";
}

How are you getting the id,i mean through Get,post or Session method?So are u sure the id is present?
Also i think it should be something like this
$query = mysql_query("SELECT * FROM pengunjung WHERE id='$id',$koneksi");
though i dont know for what are u using $koneksi.

thanks for reply,my method is POST,
$koneksi is my own variable to connect localhost,
$koneksi = mysql_connect("localhost","root","");
and i'm sure my $query is right ($query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'",$koneksi)";),
because it is totally the same script with my php tutorial reference, and i have double check it.

if i type your $query script, it will be display error...
i am sure the main problem is $id in my PHP script,

if $query script like this :
$query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'",$koneksi)";
$result = mysql_fetch_array(....bla...bla...bla...i forgot)
it didn't work. Firefox will show blank..or Firefox will show database content with id=0

but if change $id become 2, it will work!!
Firefox will show the database content with id=2

if change $id become 3, it will work!!
Firefox will show the database content with id=3

etc

it not effective right? we know it will better if id='$id', so it will be automatic, without editing my php script over and over again, just only to show one of id database content.

But it didn't work..$id cannot query,fetch_array database from phpMyAdmin to my PHP script or Firefox..

Can you help me mrniceguy?or somebody else can also help me..i appreciate your help..thanks before:)

If then u are sure the problem is with the id,try using the if(isset) function, to make sure the id is posted.
like this:

<?php
if(isset($_POST['id'])){
$id=$_POST['id'];
then retrieve info from database
}else{

echo"No id was Found";//any message to show that no id posted
}
?>

If then u are sure the problem is with the id,try using the if(isset) function, to make sure the id is posted.
like this:

<?php
if(isset($_POST['id'])){
$id=$_POST['id'];
then retrieve info from database
}else{

echo"No id was Found";//any message to show that no id posted
}
?>

Thanks mrniceguy, i've tried your script, but the result is "No id was found"
i'll give you all of of my script ,so you can check what is wrong with my script. I have 4 PHP file and 1 HTML file, there are view.php,input.htm,input.php,edit.php,and update.php

i have database with name ikc, with table name pengunjung that has 4 field,there are id,nama,email,situs.id is int with auto_increment

Here's the script for each PHP and HTM file
view.php

<?php
$host="localhost";
$user="root";
$pass="";

$koneksi=mysql_connect("$host","$user","$pass");

mysql_select_db("ikc",$koneksi);

$query=mysql_query("SELECT * FROM pengunjung",$koneksi);

$jumlah=mysql_num_rows($query);

echo "<center>List of Member registered</center>";
echo "Number of registered member : $jumlah";

while($baris=mysql_fetch_array($query))
{
echo "<br>";
echo $baris[0];
echo "<br>";
echo "Nama : ";
echo $baris[1];
echo "<br>";
echo "Email :";
echo $baris[2];
echo "<br>";
echo $baris[3];
echo "<br><a href=\"edit.php?id=".$baris[0]."\">edit</a>";
}
?>

input.htm

<html>
<head></head>
<body>
<form method="post" action="input.php">
<table>
<tr>
<td>Nama</td><td>: <input type="text" name="nama"></td>
</tr>
<tr>
<td>Email</td><td>: <input type="text" name="email"></td>
</tr>
<tr>
<td>Situs</td><td>: <input type="text" name="situs"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td><td><input type="reset" name="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>

input.php

<?php
$koneksi=mysql_connect("localhost","root","");
mysql_select_db("ikc",$koneksi);
$masuk_nama=$_POST['nama'];
$masuk_email=$_POST['email'];
$masuk_situs=$_POST['situs'];
if(isset($_POST['id'])){
$id=$_POST['id'];
}else{

echo"No id was Found";//any message to show that no id posted
}
if((!empty($masuk_nama)) && (!empty($masuk_email)) &&(!empty($masuk_situs)))
{
mysql_query("INSERT INTO pengunjung(id,nama,email,situs) VALUES ('$id','$masuk_nama','$masuk_email','$masuk_situs')",$koneksi);
echo "<br>Data has been inputed";
echo "<br>Click <a href=\"view.php\">here</a> to view the content of Database";
}
else
{
echo "<script>alert('Maaf,diisi yang lengkap ya!');javascript:history.go(-1);</script>";

}
?>

edit.php

<?php
$koneksi=mysql_connect("localhost","root","");
mysql_select_db("ikc",$koneksi);

if(isset($_POST['id']))
{
$id=$_POST['id'];
}
else
{
echo "No id was Found";
}
$query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'");
while($baris=mysql_fetch_array($query))
{
$nm=$baris['nama'];
$emil=$baris['email'];
$site=$baris['situs'];
echo "<form method=\"post\" action=\"update.php\">";
echo "Nama : <input type=\"text\" name=\"nama\" value=\"$nm\">";
echo "<br>";
echo "Email : <input type=\"text\" name=\"email\" value=\"$emil\">";
echo "<br>";
echo "Situs : <input type=\"text\" name=\"situs\" value=\"$site\">";
echo "<br>";
echo "<input type=\"submit\" name=\"submit\" value=\"update\">";
echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";
echo "</form>";
}

?>

update.php

<?php
$koneksi=mysql_connect("localhost","root","");
mysql_select_db("ikc",$koneksi);
if(isset($_POST['id'])){
$id=$_POST['id'];

}else{

echo"No id was Found";//any message to show that no id posted
}
$edit_nama=$_POST['nama'];
$edit_email=$_POST['email'];
$edit_situs=$_POST['situs'];
$query=mysql_query("UPDATE pengunjung set id='$id',nama='$edit_nama',email='$edit_email',situs='$edit_situs' where id='$id'");
echo "Data dengan id=$id telah diupdate";
?>

The homepage is input.htm, fill the form, but then input.php shows that No id was found

then view.php shows the new member was added to the database,each of members has option to edit at edit.php, but i cannot edit each member database through my php script.

you can try all of the script above,mrniceguy, if you wanna try.
I hope you can find out what the problem solution..

Any solution again?

@adindra here it is.
Firstly i Renamed ur input.html to index.php and also some little changes to other files as below:
input.php

<?php
 
      $koneksi=mysql_connect("localhost","root","");

      mysql_select_db("ikc",$koneksi);

      $masuk_nama=$_POST['nama'];

      $masuk_email=$_POST['email'];

      $masuk_situs=$_POST['situs'];
  
     if((!empty($masuk_nama)) && (!empty($masuk_email)) &&(!empty($masuk_situs)))

      {

      mysql_query("INSERT INTO pengunjung(nama,email,situs) VALUES ('$masuk_nama','$masuk_email','$masuk_situs')",$koneksi);//you dont need to insert the id as it is auto increment so i removed it here.

      echo "<br>Data has been inputed";

      echo "<br>Click <a href=\"view.php\">here</a> to view the content of Database";

      }

      else

      {

      echo "<script>alert('Maaf,diisi yang lengkap ya!');javascript:history.go(-1);</script>";

       
 
      }

      ?>

view.php

<?php

      $host="localhost";

      $user="root";

      $pass="";

       $koneksi=mysql_connect("$host","$user","$pass");

      mysql_select_db("ikc",$koneksi);

   $query=mysql_query("SELECT * FROM pengunjung",$koneksi);

   $jumlah=mysql_num_rows($query);

    echo "<center>List of Member registered</center>";

      echo "Number of registered member : $jumlah";

     while($baris=mysql_fetch_array($query))

      {

      echo "<br>";

      echo $baris[0];

      echo "<br>";
 
      echo "Nama : ";

      echo $baris[1];

      echo "<br>";

      echo "Email :";
 
      echo $baris[2];

      echo "<br>";
 
      echo $baris[3];

      echo "<br><a href=\"edit.php?id=".$baris[0]."\">edit</a>";

      }

      ?>

edit.php

<?php

      $koneksi=mysql_connect("localhost","root","");

      mysql_select_db("ikc",$koneksi);
    if(isset($_GET['id']))//this is method GET and not POST,as we are getting the value from the link.

    {

    $id=$_GET['id'];

    $query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'");

      while($baris=mysql_fetch_array($query))

      {

      $nm=$baris['nama'];

      $emil=$baris['email'];
  
      $site=$baris['situs'];

      echo "<form method=\"post\" action=\"update.php\">";
 
      echo "Nama : <input type=\"text\" name=\"nama\" value=\"$nm\">";
 echo "<br>";

      echo "Email : <input type=\"text\" name=\"email\" value=\"$emil\">";

      echo "<br>";

      echo "Situs : <input type=\"text\" name=\"situs\" value=\"$site\">";

      echo "<br>";

      echo "<input type=\"submit\" name=\"submit\" value=\"update\">";

      echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";

      echo "</form>";

      }

      }else

      {

      echo "No id was Found";//any message to show that we didn`t Get any id.

      } 

      ?>

update.php

<?php
 
      $koneksi=mysql_connect("localhost","root","");
 
      mysql_select_db("ikc",$koneksi);
 
      if(isset($_POST['id'])){
  
      $id=$_POST['id'];
      $edit_nama=$_POST['nama'];
      $edit_email=$_POST['email'];
      $edit_situs=$_POST['situs'];

      $query=mysql_query("UPDATE pengunjung set nama='$edit_nama',email='$edit_email',situs='$edit_situs' WHERE id='$id'");//here u dont need to update the id so i removed it.

      echo "Data dengan id=$id telah diupdate</br>";
	  echo"click <a href=view.php>here</a> to view the datas";
    }else{

       

      echo"No id was Found";//any message to show that no id posted

      }
	  
	  
	  
      ?>

it tested it in my localhost server and its working.

@adindra here it is.
Firstly i Renamed ur input.html to index.php and also some little changes to other files as below:
input.php

<?php
 
      $koneksi=mysql_connect("localhost","root","");

      mysql_select_db("ikc",$koneksi);

      $masuk_nama=$_POST['nama'];

      $masuk_email=$_POST['email'];

      $masuk_situs=$_POST['situs'];
  
     if((!empty($masuk_nama)) && (!empty($masuk_email)) &&(!empty($masuk_situs)))

      {

      mysql_query("INSERT INTO pengunjung(nama,email,situs) VALUES ('$masuk_nama','$masuk_email','$masuk_situs')",$koneksi);//you dont need to insert the id as it is auto increment so i removed it here.

      echo "<br>Data has been inputed";

      echo "<br>Click <a href=\"view.php\">here</a> to view the content of Database";

      }

      else

      {

      echo "<script>alert('Maaf,diisi yang lengkap ya!');javascript:history.go(-1);</script>";

       
 
      }

      ?>

view.php

<?php

      $host="localhost";

      $user="root";

      $pass="";

       $koneksi=mysql_connect("$host","$user","$pass");

      mysql_select_db("ikc",$koneksi);

   $query=mysql_query("SELECT * FROM pengunjung",$koneksi);

   $jumlah=mysql_num_rows($query);

    echo "<center>List of Member registered</center>";

      echo "Number of registered member : $jumlah";

     while($baris=mysql_fetch_array($query))

      {

      echo "<br>";

      echo $baris[0];

      echo "<br>";
 
      echo "Nama : ";

      echo $baris[1];

      echo "<br>";

      echo "Email :";
 
      echo $baris[2];

      echo "<br>";
 
      echo $baris[3];

      echo "<br><a href=\"edit.php?id=".$baris[0]."\">edit</a>";

      }

      ?>

edit.php

<?php

      $koneksi=mysql_connect("localhost","root","");

      mysql_select_db("ikc",$koneksi);
    if(isset($_GET['id']))//this is method GET and not POST,as we are getting the value from the link.

    {

    $id=$_GET['id'];

    $query = mysql_query("SELECT * FROM pengunjung WHERE id='$id'");

      while($baris=mysql_fetch_array($query))

      {

      $nm=$baris['nama'];

      $emil=$baris['email'];
  
      $site=$baris['situs'];

      echo "<form method=\"post\" action=\"update.php\">";
 
      echo "Nama : <input type=\"text\" name=\"nama\" value=\"$nm\">";
 echo "<br>";

      echo "Email : <input type=\"text\" name=\"email\" value=\"$emil\">";

      echo "<br>";

      echo "Situs : <input type=\"text\" name=\"situs\" value=\"$site\">";

      echo "<br>";

      echo "<input type=\"submit\" name=\"submit\" value=\"update\">";

      echo "<input type=\"hidden\" name=\"id\" value=\"$id\">";

      echo "</form>";

      }

      }else

      {

      echo "No id was Found";//any message to show that we didn`t Get any id.

      } 

      ?>

update.php

<?php
 
      $koneksi=mysql_connect("localhost","root","");
 
      mysql_select_db("ikc",$koneksi);
 
      if(isset($_POST['id'])){
  
      $id=$_POST['id'];
      $edit_nama=$_POST['nama'];
      $edit_email=$_POST['email'];
      $edit_situs=$_POST['situs'];

      $query=mysql_query("UPDATE pengunjung set nama='$edit_nama',email='$edit_email',situs='$edit_situs' WHERE id='$id'");//here u dont need to update the id so i removed it.

      echo "Data dengan id=$id telah diupdate</br>";
	  echo"click <a href=view.php>here</a> to view the datas";
    }else{

       

      echo"No id was Found";//any message to show that no id posted

      }
	  
	  
	  
      ?>

it tested it in my localhost server and its working.

YES!! It works mrniceguy....

hahaha...so my error is the method..i forgot that method GET can make id in URL link became varibel $id..

Thank you very much Mrniceguy...

you are really a nice guy...

PROBLEM SOLVED

next time i'll ask you again if i need help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.