1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Nothing appear

Discussion in 'PHP' started by davenet, Apr 7, 2007.

  1. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #21
    New 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 new error message:

    
    Warning: copy(NULL) [function.copy]: failed to open stream: No such file or directory in /home/davenet/public_html/php/input_data.php on line 73
    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, NULL)' at line 7
    
    PHP:
    Line 73 is copy ($foto, "../image/$foto");

    What should I change that code to?
     
    davenet, Apr 21, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #22
    You shouldn't change that line of code. The problem goes back to the same one which has been the cause of everything else: missing or incorrect input paramters to the page.

    You're halfway towards validating the input correctly with all the "if" statements at the top of the file, but setting all the defaults to '' is the problem. Most of those parameters seem to need more logical defaults.

    Instead of simply setting all your variables to '' you need to display an error message to the user and redisplay the form. You should not continue doing things if any of those variables contain invalid values, and from the looks of your code and MySQL database schema, emtpy/null strings are invalid values.

    This is how I often handle these things:

    
    // default to no error message
    $message = '';
    
    if (!$_POST['foto'])
       $message .= 'Required parameter foto is missing.<br />';
    else
       $foto = $_POST['foto'];
    
    if (!$_POST['nim'])
       $message .= 'Required parameter nim is missing.<br />';
    else
       $nim = $_POST['nim'];
    
    //etc. for each parameter
    
    if ($message)  // we have an error, so display it and redisplay the form
    {
       echo $message;
       // do whatever
    }
    else  // no errors, so process the data
    {
        copy(....);
        call_mysql_whatever(....);
    } 
    
    
    PHP:
    You'll need to modify that to fit into your page correctly, especially the big "if" block.

    Notice that for each missing parameter, the message string grows longer because of the .= assignment.
     
    sea otter, Apr 21, 2007 IP
  3. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    Here is my database schema:
    ------------------------------------------------------------

    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;

    -------------------------------------------------------

    And I have this for my new revision:

    
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    include ("server.php");
    include ("menu.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());
    
    
    
    // default to no error message
    $message = '';
    if (!$_POST['nim'])   
    $message .= 'Required parameter nim is missing.<br />';
    else   $nim = $_POST['nim'];
    if (!$_POST['nama'])   
    $message .= 'Required parameter nama is missing.<br />';
    else   $nama = $_POST['nama'];
    if (!$_POST['jk'])   
    $message .= 'Required parameter jk is missing.<br />';
    else   $jk = $_POST['jk'];
    if (!$_POST['jurusan'])   
    $message .= 'Required parameter jurusan is missing.<br />';
    else   $jurusan = $_POST['jurusan'];
    if (!$_POST['foto'])   
    $message .= 'Required parameter foto is missing.<br />';
    else   $foto = $_POST['foto'];
    //etc. for each parameter
    if ($message)  // we have an error, so display it 
    {   echo $message;   // do whatever
    
    // and redisplay the form
    
    
    
    }else  // no errors, so process the data
    {    
    // 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());
    }
    
    ?>
    
    PHP:
    and I have this for output:

    
    [ Input Data Mahasiswa ] [ Input Nilai Mahasiswa ] [ Daftar Mahasiswa ] [ Data Jurusan ] [ Input Data Jurusan ] 
    Notice: Undefined variable: nama in /home/davenet/public_html/php/input_data.php on line 11
    
    Notice: Undefined index: foto in /home/davenet/public_html/php/input_data.php on line 32
    Required parameter foto is missing.
    
    PHP:
    I receive some error message. I wonder is there any way to redisplay the form just by quoting the form file name (input_data_mhs.php) or do I have to copy and paste the whole code to (input_data.php)?


    It appears that the error message in:

    Notice: Undefined variable: nama in /home/davenet/public_html/php/input_data.php on line 11

    has to do with:
    $nama = ucwords($nama);

    I thought I already type in nama field correctly which is char(25). but they still display the error or maybe I just should revise "$nama = ucwords($nama);" code?

    I have no idea what to do with the error message in foto line 32. since I thought I already type in a file name (char(15)).

    Thanks for help.
     
    davenet, Apr 22, 2007 IP