unable to record data

Discussion in 'PHP' started by davenet, May 3, 2007.

  1. #1
    Dear all,

    I have the following code:

    
    <html>
    <head><title>Input Data Mahasiswa</title></head>
    <body>
    <?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>
    ";
    
    ?>
    
    
    </body>
    </html>
    
    PHP:
    and

    
    <?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
    
    include ("input_data_mhs_form.php");
    
    }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`,
    `jurusan`,
    `foto`)
    VALUES (`$nim`, `$nama`, `$jk`, `$jurusan`, `$foto`)", $koneksi)
    or die (mysql_error());
    }
    
    ?>
    
    PHP:
    everytime I enter data I receive this error:

    
    [ Input Data Mahasiswa ] [ Input Nilai Mahasiswa ] [ Daftar Mahasiswa ] [ Data Jurusan ] [ Input Data Jurusan ] 
    Notice: Undefined index: foto in /home/davenet/public_html/php/input_data.php on line 32
    Required parameter foto is missing.
    
    Input Data Mahasiswa
    Nim  
    Nama  
    Jenis Kelamin Laki 
     Perempuan 
    Angkatan  Tahun200720062005200420032002  
    Jurusan  ---Program Studi--- 
    Foto   
    
    
    PHP:
    I entered photo file *.jpg or *.png which I thought should match varchar(15) for foto.
    and when I checked on phpmyAdmin none of the datas are recorded. Could anyone offer help in solving this problem?
    appreciate each progression.
     
    davenet, May 3, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    You have posted foto from a file field so it will be available under $_FILES array,

    so the new check will be

    if (!$_FILES['foto'])
    instead of
    if (!$_POST['foto'])

    Hope this helps.
     
    designcode, May 3, 2007 IP
  3. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #3
    as a tip Davenet, why you don't use Heredoc operator (<<<) instead of escaping so many " ?
     
    gibex, May 4, 2007 IP
  4. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't quite understand how to operate Heredoc operator Gibex, how do you do it?

    Also, this is my new revision:

    input_data.php
    
    
    <?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 (!$_FILES['foto'])
    $message .= 'Required parameter foto is missing.<br />';
    else   $foto = $_FILES['foto'];
    //etc. for each parameter
    if ($message)  // we have an error, so display it 
    {   echo $message;   // do whatever
    
    // and redisplay the form
    
    include ("input_data_mhs_form.php");
    
    }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`,
    `jurusan`,
    `foto`)
    VALUES (`$nim`, `$nama`, `$jk`, `$jurusan`, `$foto`)", $koneksi)
    or die (mysql_error());
    }
    
    ?>
    
    
    PHP:
    I receive this error message:

    
    [ Input Data Mahasiswa ] [ Input Nilai Mahasiswa ] [ Daftar Mahasiswa ] [ Data Jurusan ] [ Input Data Jurusan ] 
    Notice: Array to string conversion in /home/davenet/public_html/php/input_data.php on line 46
    
    Warning: copy(Array) [function.copy]: failed to open stream: No such file or directory in /home/davenet/public_html/php/input_data.php on line 46
    Unknown column '0100910054' in 'field list'
    
    PHP:
    Line 46 is copy ($foto, "../image/$foto");

    I have new image folder in the same folder where input_data.php is stored.
     
    davenet, May 4, 2007 IP
  5. sky22

    sky22 Guest

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You need to use move_uploaded_file and you need to add ['tmp_name'] to get the temporary file.

    This should work:
    move_uploaded_file( $_FILES['foto']['tmp_name'], '../image/' . $_FILES['foto']['name'] )
    PHP:
    In place of this:

    copy ($foto, "../image/$foto");
    PHP:
    more information about handling file uplaods: http://uk2.php.net/features.file-upload
     
    sky22, May 4, 2007 IP
  6. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    My new revision for:

    input_data.php
    
    <?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 (!$_FILES['foto'])
    $message .= 'Required parameter foto is missing.<br />';
    else   $foto = $_FILES['foto'];
    //etc. for each parameter
    if ($message)  // we have an error, so display it 
    {   echo $message;   // do whatever
    
    // and redisplay the form
    
    include ("input_data_mhs_form.php");
    
    }else  // no errors, so process the data
    {    
    // Copy file ke dalam folder ../image
    move_uploaded_file( $_FILES['foto']['tmp_name'], '../image/' . $_FILES['foto']['name'] )
    
    echo $nim;
    echo $nama;
    echo $jk;
    echo $jurusan;
    echo $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`,
    `jurusan`,
    `foto`)
    VALUES (`$nim`, `$nama`, `$jk`, `$jurusan`, `$foto`)", $koneksi)
    or die (mysql_error());
    }
    
    ?>
    
    
    PHP:
    Some where in the code I typed:
    echo $nim;
    echo $nama;
    echo $jk;
    echo $jurusan;
    echo $foto;

    To see what variables are recorded. Whenever I clicked submit in input_data_mhs.php (you can checked the code on my first post on this thread) nothing appear on input_data.php. I also have checked in phpmyAdmin to see if they are recorded, yet they are not.
     
    davenet, May 4, 2007 IP
  7. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks. and any further help? :)
     
    davenet, May 5, 2007 IP
  8. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #8
    Heredoc operator in your case

    instead of

    
    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>";
    
    
    PHP:
    use

    
    
    echo <<<EOT
            <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>
    EOT;
    
    PHP:
    be sure you don't have spaces after <<<EOT and after EOT;
     
    gibex, May 12, 2007 IP
  9. davenet

    davenet Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Do I have to change to EOT for every echo?

    I revise the code to this, Gibex:

    
    <html>
    <head><title>Input Data Mahasiswa</title></head>
    <body>
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    include ("server.php");
    include ("menu.php");
    
    echo <<<EOT
    		<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>
    EOT;
    
    echo <<<EOT
    		<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>
    EOT;		
    
    echo <<<EOT
    	<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>
    EOT;		
    		
    echo <<<EOT
    	<tr>
    		<td>Jurusan</td>
    		<td><select name="jurusan">
    		<option value="kosong">---Program Studi---
    </option>
    EOT;	
    
    
    $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 <<<EOT
    	<option value = "$row_nim[0]" > $row_nim[1]
    	</option>
    	EOT
    	}
    		echo "</td>";
    echo <<<EOT
    	<tr>
    		<td>Foto</td>
    		<td><input type="FILE" accept=image/jpeg name="foto"></td></tr>
    	EOT	
    		
    echo <<<EOT
    	<tr>
    	<td><input type="submit" value="submit"></td>
    	<td><input type="reset" value="reset"></td>
    	</tr>
    	EOT
    	
    </table>
    ";
    
    ?>
    
    
    </body>
    </html>
    
    
    PHP:
    Yet, now nothing appear. Before I can see the form eventhough I cannot record the data and print them out to the next page (input_data.php)

    Maybe you can check the result: http://www.davesolution.com/xYzAbJ/input_data_mhs.php
     
    davenet, May 12, 2007 IP