The following codes does not work. Nothing appear on the screen whenever I run the code, why? <? include ("server.php"); include ("menu.php"); echo " <h1 align=\"center\">Input Nilai Mahasiswa</h1> <form method=\"post\" action=\"$PHP_SELF\"> <h3 align=\"center\">$nim</h3> <table border=\"0\" align=\"center\"> <tr> <td>Nim</td> <td> <select onchange=\"this.form.submit()\">"; $koneksi = mysql_connect($host, $user, $pass) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); <option value=\"kosong\">::::Nim::::</option>"; while ($row = mysql_fetch_array($query)) { // $row[0] = nim // $nim diperoleh jika tag select di scroll if ($nim==$row[0]) echo "<option value = \"$row[0]\" selected>$row[0] </option>"; else { echo "<option value = \"$row[0]\">$row[0]</option>"; } } echo "</select></td></tr>"; $query_mhs = mysql_query("select mhs.nim, mhs.nama, mhs.jk, mhs.nilai, mhs.grade, mhs.foto, jurusan.nama_jurusan from mhs, jurusan where mhs.jurusan=jurusan.kode_jurusan and nim='$nim' order by nim", $koneksi) or die (mysql_error()); $row_mhs = mysql_fetch_array($query_mhs); /* $row_mhs[0] = nim $row_mhs[1] = nama $row_mhs[2] = jk $row_mhs[3] = nilai $row_mhs[4] = grade $row_mhs[5] = foto $row_mhs[6] = jurusan */ echo " <tr> <td>Nama</td> <td>$row_mhs[1]</td> </tr>"; echo "<tr> <td>Nilai</td> <td> <input type=\"text\" name=\"nilai\"> </td> </tr>"; echo "<tr>"; echo " <td>Jurusan</td> <td>$row_mhs[6]</td></tr>"; echo " <tr> <td> <input type=\"submit\" name=\"kirim\" value=\"kirim\" > </td> </tr>"; echo "</table>"; echo "<img src=\"../image/$row_mhs[5]\" alt=\"$row_mhs[1]\" align=\"center\"> </form>"; if ($kirim) { if ($nilai>="80") $grade = "A"; else if (($nilai >= "70") and ($nilai <= "79")) $grade = "B"; else if (($nilai >= "60") and ($nilai <= "69")) $grade = "C"; else if (($nilai >= "50") and ($nilai <= "59")) $grade = "D"; else if ($nilai < "50") $grade = "E"; $query_update = mysql_query ("update mhs set nilai='$nilai', grade='$grade' where nim='$nim'", $koneksi) or die (mysql_error()); } ?> PHP: Appreciate comments.
First, try replacing the short PHP tags with the standard ones. (<?php). They might be disabled by the host and your code won't be parsed. Then try setting this right under the PHP tags if the above doesn't work. error_reporting(E_ALL); ini_set('display_errors', '1'); PHP:
I have replaced the previous code with this: <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include ("server.php"); include ("menu.php"); echo " <h1 align=\"center\">Input Nilai Mahasiswa</h1> <form method=\"post\" action=\"$PHP_SELF\"> <h3 align=\"center\">$nim</h3> <table border=\"0\" align=\"center\"> <tr> <td>Nim</td> <td> <select onchange=\"this.form.submit()\">"; $koneksi = mysql_connect($host, $user, $pass) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); <option value=\"kosong\">::::Nim::::</option>"; while ($row = mysql_fetch_array($query)) { // $row[0] = nim // $nim diperoleh jika tag select di scroll if ($nim==$row[0]) echo "<option value = \"$row[0]\" selected>$row[0] </option>"; else { echo "<option value = \"$row[0]\">$row[0]</option>"; } } echo "</select></td></tr>"; $query_mhs = mysql_query("select mhs.nim, mhs.nama, mhs.jk, mhs.nilai, mhs.grade, mhs.foto, jurusan.nama_jurusan from mhs, jurusan where mhs.jurusan=jurusan.kode_jurusan and nim='$nim' order by nim", $koneksi) or die (mysql_error()); $row_mhs = mysql_fetch_array($query_mhs); /* $row_mhs[0] = nim $row_mhs[1] = nama $row_mhs[2] = jk $row_mhs[3] = nilai $row_mhs[4] = grade $row_mhs[5] = foto $row_mhs[6] = jurusan */ echo " <tr> <td>Nama</td> <td>$row_mhs[1]</td> </tr>"; echo "<tr> <td>Nilai</td> <td> <input type=\"text\" name=\"nilai\"> </td> </tr>"; echo "<tr>"; echo " <td>Jurusan</td> <td>$row_mhs[6]</td></tr>"; echo " <tr> <td> <input type=\"submit\" name=\"kirim\" value=\"kirim\" > </td> </tr>"; echo "</table>"; echo "<img src=\"../image/$row_mhs[5]\" alt=\"$row_mhs[1]\" align=\"center\"> </form>"; if ($kirim) { if ($nilai>="80") $grade = "A"; else if (($nilai >= "70") and ($nilai <= "79")) $grade = "B"; else if (($nilai >= "60") and ($nilai <= "69")) $grade = "C"; else if (($nilai >= "50") and ($nilai <= "59")) $grade = "D"; else if ($nilai < "50") $grade = "E"; $query_update = mysql_query ("update mhs set nilai='$nilai', grade='$grade' where nim='$nim'", $koneksi) or die (mysql_error()); } ?> PHP: For additional information, I also have the following tables on my sql server: ------------------------------------------------------------ CREATE TABLE 'jurusan' ( 'kode_jurusan' char(3) NOT NULL default '', 'nama_jurusan' char(25) NOT NULL default '', PRIMARY KEY ('kode_jurusan') ) TYPE=MyISAM; CREATE TABLE 'mhs' ( 'nim' char(10) NOT NULL default '', 'nama' char(25) NOT NULL default '', 'jk' enum ('1', '0') NOT NULL default '0', 'nilai' int(3) NOT NULL default '0', 'grade' char(1) NOT NULL default '', 'jurusan' char(3) NOT NULL default '', 'foto' char(15) default NULL, PRIMARY KEY ('nim') ) TYPE=MyISAM; ------------------------------------------------------- The result is still the same. any more ideas?
Try to control F5 which will refresh without cache. Also view source and see if there is anything there. If error reporting is on, it has to error. And from your code - there should definitely be text echod.
line 24, change this <option value=\"kosong\">::::Nim::::</option>"; PHP: to this echo "<option value=\"kosong\">::::Nim::::</option>"; PHP: After doing that, the code passes the php lint syntax checker (from the command line: php -l scriptname) Note that the parameter is "minus el".
Thanks all. Now, I get the following output: Notice: Undefined variable: PHP_SELF in /home/davenet/public_html/php/input_nilai_mhs.php on line 12 Notice: Undefined variable: nim in /home/davenet/public_html/php/input_nilai_mhs.php on line 13 Input Nilai Mahasiswa Nim ::::Nim:::: Notice: Undefined variable: query in /home/davenet/public_html/php/input_nilai_mhs.php on line 25 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/davenet/public_html/php/input_nilai_mhs.php on line 25 Notice: Undefined variable: nim in /home/davenet/public_html/php/input_nilai_mhs.php on line 42 Nama Nilai Jurusan Notice: Undefined variable: kirim in /home/davenet/public_html/php/input_nilai_mhs.php on line 88 PHP: Can anyone suggest how to fix the errors?
Notices are quite significant indeed, especially in the case of the ones we have here. PHP_SELF isn't a variable; it's an entry in the $_SERVER array, and so must be referred to as $_SERVER['PHP_SELF']; Change line 12 from this <form method=\"post\" action=\"$PHP_SELF\"> PHP: to this <form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\"> PHP: As for the next error RE $nim, you're using a variable that has not been declared or initialized anywhere. Obviously, you intend for it to have some value, so the question is, where do you set it (or where did you mean to set it? In server.php? In menu.php? Same goes for the variable $kirim. Where did you mean to set it? Let's start with those.
No offence jitesh, but do you EVER read the threads that you reply to? You seem to have a habit of just jumping in to threads late with replies that have been made a long time before you got there. Would it hurt you to read what's already been written or maybe ask for you to CONTRIBUTE to the discussion as opposed to just apparently parroting other people? I know that I'm not contributing myself to this thread but man... it needs to be said! Oh, and major apologies to davenet for just 'semi-hijacking' his thread. If it helps, I would suggest that your code seems to assume that register_globals is on. I would check to see if it's been turned off and if so, either turn it on or rewrite your code to assume it's not on. I strongly recommend the latter for two reasons: 1) it's a lot more secure; and 2) register_globals is being dropped eventually, anyway, so you may as well get used to writing the code now.
True. Professionally written code should not leave any E_NOTICES or warnings. Not even if error reporting is set to E_ALL. Each time a notice or warning appears, it's written to the error log, which means, open file, write, close. So your code executes slower. It should check for all possible errors and deal with them before they have a chance to appear.
menu.php basically shows me links of other forms where one of them I input $nim: input_data_mhs.php <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include ("server.php"); include ("menu.php"); echo " <h1 align=\"center\">Input Data Mahasiswa</h1> <form method=\"post\" action=\"input_data.php\" ENCTYPE=\"MULTIPART/FORM-DATA\"> <table border=\"0\" align=\"center\"> <tr> <td>Nim</td> <td><input type=\"text\" name=\"nim\"></td> </tr> <tr> <td>Nama</td> <td><input type=\"text\" name=\"nama\"></td> </tr>"; echo " <tr> <td>Jenis Kelamin</td> <td><input type=\"radio\" name=\"jk\" value\"1\">Laki</td> </tr> <tr> <td></td> <td><input type=\"radio\" name=\"jk\" value=\"0\">Perempuan</td> </tr>"; echo " <tr> <td>Angkatan</td> <td><select name=\"angkatan\"> <option value=\"kosong\"> Tahun </option>"; $tahun = (integer) date ("Y"); for ($i = $tahun; $i>($tahun - 6); $i--) { echo "<option value=\"$i\">$i"; } echo "</option> </select><td></tr>"; echo " <tr> <td>Jurusan</td> <td><select name=\"jurusan\"> <option value=\"kosong\">---Program Studi--- </option>"; $koneksi = mysql_connect($host, $user, $pass) or die (mysql_error()); mysql_select_db($db, $koneksi) or die (mysql_error()); $query = mysql_query("select * from jurusan order by kode_jurusan", $koneksi) or die (mysql_error()); while ($row_nim = mysql_fetch_array($query)) { echo "<option value = \"$row_nim[0]\" > $row_nim[1] </option>"; } echo "</td>"; echo " <tr> <td>Foto</td> <td><input type=\"FILE\" accept=image/jpeg name=\"foto\"></td></tr>"; echo " <tr> <td><input type=\"submit\" value=\"submit\"></td> <td><input type=\"reset\" value=\"reset\"></td> </tr> </table> "; ?> PHP: After I type in the form in input_data_mhs.php then it will execute: input_data.php <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include ("server.php"); // kata yang di ucwords() akan mempunyai huruf pertamanya // huruf kapital // walaupun sengaja di tulis dalam huruf kecil $nama = ucwords($nama); $koneksi = mysql_connect ($host, $user, $pass) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); // Copy file ke dalam folder ../image copy ($foto, "../image/$foto_name"); // Karena hanya memasukkan informasi mahasiswa maka // untuk pengisian nilai dan grade di buatkan form tersendiri // sehingga pada file ini dikosongi mysql_query ("insert into mhs values ('', '$nim', '$nama', '$jk', '', '', '$jurusan', '$foto_name')", $koneksi) or die (mysql_error()); include ("menu.php"); ?> $nama = ucwords($nama); PHP: and it shows me this result: Column count doesn't match value count at row 1 PHP: and the same errors still appears in input_nilai_mhs.php, which is $nim and $kirim. $kirim basically is a button to send the form information to the database. and I have this code in server.php <? $host = "localhost"; $user = "davenet_davenet"; $pass = "******"; $db = "davenet_php"; ?> PHP:
It means the number of items in your INSERT statement doesn't match the number of items in your database table, and/or the parameters in your VALUE clause do not match the order in which the parameters are declared in your table. Basically, you're trying to asign a value that isn't an empty string to an auto-incremented field (typically called 'id'). Try using the more detailed INSERT syntax which explicitly names the columns, and make sure the arguments in VALUES are in the same order, e.g. INSERT into mhs ('id', 'col_name2', 'col_name3') VALUES ('','val2','val3'); Code (markup): Note that 'id' gets an empty string as a value, so that it is allowed to autoincrement on the insert (if your autoincrement column is named something else, use the correct name).
I revise input_data.php to this: <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include ("server.php"); $nim = $_POST['nim']; $nama = $_POST['nama']; $jk = $_POST['jk']; $jurusan = $_POST['jurusan']; $foto = $_POST['foto']; // kata yang di ucwords() akan mempunyai huruf pertamanya // huruf kapital // walaupun sengaja di tulis dalam huruf kecil $nama = ucwords($nama); $koneksi = mysql_connect ($host, $user, $pass) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); // Copy file ke dalam folder ../image copy ($foto, "../image/$foto"); // Karena hanya memasukkan informasi mahasiswa maka // untuk pengisian nilai dan grade di buatkan form tersendiri // sehingga pada file ini dikosongi mysql_query ("INSERT into mhs ('nim', 'nama', 'jk', 'angkatan', 'jurusan', 'foto') VALUES ('$nim', '$nama', '$jk', '', '$jurusan', '$foto')", $koneksi) or die (mysql_error()); include ("menu.php"); ?> PHP: Now, I receive the following error: Notice: Undefined index: foto in /home/davenet/public_html/php/input_data.php on line 11 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''nim', 'nama', 'jk', 'angkatan', 'jurusan', 'foto') VALUES ('0100910054', ' at line 1 PHP: Line 11 is $foto = $_POST['foto']; more ideas?
$_POST['foto'] doesn't exist, which means it wasn't passed in with the form post. You need to check for this. if (isset($_POST['foto'])) { $foto = $_POST['foto']; } else { // handle error here, or set $foto to some default } PHP: You need to do this for each of the POST variables.
Another revision of input_data.php: <?php error_reporting(E_ALL); ini_set('display_errors', '1'); include ("server.php"); //$nim = $_POST['nim']; //$nama = $_POST['nama']; //$jk = $_POST['jk']; //$jurusan = $_POST['jurusan']; if (isset($_POST['nim'])) { $nim = $_POST['nim'];} else{ // handle error here, or set $foto to some default $nim = '';} if (isset($_POST['nama'])) { $nama = $_POST['nama'];} else{ // handle error here, or set $foto to some default $nama = '';} if (isset($_POST['jk'])) { $jk = $_POST['jk'];} else{ // handle error here, or set $foto to some default $jk = '0';} if (isset($_POST['nilai'])) { $nilai = $_POST['nilai'];} else{ // handle error here, or set $foto to some default $nilai = '0';} if (isset($_POST['grade'])) { $grade = $_POST['grade'];} else{ // handle error here, or set $foto to some default $grade = '';} if (isset($_POST['jurusan'])) { $jurusan = $_POST['jurusan'];} else{ // handle error here, or set $foto to some default $jurusan = '';} if (isset($_POST['foto'])) { $foto = $_POST['foto'];} else{ // handle error here, or set $foto to some default $foto = NULL;} // kata yang di ucwords() akan mempunyai huruf pertamanya // huruf kapital // walaupun sengaja di tulis dalam huruf kecil $nama = ucwords($nama); $koneksi = mysql_connect ($host, $user, $pass) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); // Copy file ke dalam folder ../image copy ($foto, "../image/$foto"); // Karena hanya memasukkan informasi mahasiswa maka // untuk pengisian nilai dan grade di buatkan form tersendiri // sehingga pada file ini dikosongi mysql_query ("INSERT into mhs ('nim', 'nama', 'jk', 'angkatan', 'jurusan', 'foto') VALUES ('$nim', '$nama', '$jk', '', '$jurusan', '$foto')", $koneksi) or die (mysql_error()); include ("menu.php"); ?> PHP: and You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''nim', 'nama', 'jk', 'angkatan', 'jurusan', 'foto') VALUES ('0100910054', ' at line 1 PHP: I still have no clue how to fix the SQL syntax. From the error message it seems like they recognize the VALUES. I tried to change the SQL syntax several times by trying delete the ' mark or the variables, and the result is still remains. any more suggestion?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Syafii, on, , kosong, )' at line 7 PHP: now, this massage appear and the data is not yet recorded.