This question already has answers here:
Move_uploaded_file() function is not working
(18 answers)
What does enctype='multipart/form-data' mean?
(9 answers)
Closed 3 months ago.
I'm making a function to insert some items to my database table but the images have to be saved locally to once uploaded. When I try doing that it keeps getting the error that it's not possible to upload it. Why's that?
<?php
if (isset($_GET['action']) and $_GET['action'] == 'add') { ?>
<div>
<form method='post' action='upload.php?action=add'>
<h2>Add new songs</h2>
<input type="hidden" name="action" value="insert">
<input type='text' name='title' class='textInput' placeholder='song title'>
<input type='text' name='composer' class='textInput' placeholder='composer'>
<input type='text' name='genre' class='textInput' placeholder='genre'>
<input type='text' name='instrument1' class='textInput' placeholder='instrument1'>
<input type='text' name='instrument2' class='textInput' placeholder='instrument2'>
<input type='text' name='instrument3' class='textInput' placeholder='instrument3'>
<input type='text' name='instrument4' class='textInput' placeholder='instrument4'>
<input type='text' name='instrument5' class='textInput' placeholder='instrument5'>
<div>
<label for="pngSheet">Image PNG file</label>
<input type="file" name='pngSheet' accept='.png'>
</div>
<div>
<label for="xmlSheet">Music XML file</label>
<input type="file" name='xmlSheet' accept='.musicxml'>
</div>
<input type='submit' name='submit' value='Add'>
</form>
</div>
<?php }
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['action'] == 'insert') {
$getTitle = $_POST['title'];
$getComposer = $_POST['composer'];
$getGenre = $_POST['genre'];
$getImgSheet = $_POST['pngSheet'];
$getSheet = $_POST['xmlSheet'];
$location = '../img/' . $getImgSheet;
if (move_uploaded_file($_POST['pngSheet'], $location)) {
$query = "INSERT INTO `imslp_sheets`(`sheets_title`, `sheets_composer`, `sheets_genre`, `sheets_img`,`sheets_xml`) VALUES ('$getTitle', '$getComposer', '$getGenre', '$getImgSheet','$getSheet')";
$result = $conn->query($query);
} else {
echo "Error uploading file $getImgSheet";
}
}
}
?>
<?php
$conn=mysqli_connect("localhost","root","","fesdb");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_POST['Insert']))
{
$txt = $_POST['text1'];
}
{
$query = "insert into table (Mytext) VALUES ('$txt')";
$result = mysqli_query($conn,$query);
if($result)
{
echo '<script>alert("record inserted")</script>';
}
else
{
echo ' Please Check Your Query ';
}
}
?>
<html>
<body>
<form action="" id="myForm" >
<input type='text' name="text1"><br>
<input type='text' name="text2"><br>
<input type='text' name="text3"><br>
<input type='text' name="text4"><br>
<input type='text' name="text5"><br>
<input type='text' name="text6"><br>
<input type='text' name="text7"><br>
<input type='text' name="text8"><br>
<input type='text' name="text9"><br>
<input type='text' name="text10"><br>
<input type='submit' name="Insert">
</form>
</body>
</html>
I am a beginner in PHP .. I have sql database with one Table , this table have one column .... I have 10 textboxes in my page with one button ... I want to insert each textbox into the table as separated record ... I know I should use loop (for each ...) to solve this problem ... but I dont know how do that ...
if possible help me with a sample
Actually I need to echoed the form and get the form data in the same php page or send to another php page,A pseudo code is prepared as i cannot post my original code here .
<?php echo "<html><body>";
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
echo"<form action=$_SERVER['PHP_SELF']>
First name:<br>
<input type='text' name='firstname' value='John'><br>
Last name:<br>
<input type='text' name='lastname' value='Rambo'><br><br>
<input type='submit' value='Submit'>
</form></body></html>";
?>
The php page is call/loaded successfully loaded .The form is also working.But
form action=$_SERVER['PHP_SELF'] is not working as well as the code below is also not working.
if(isset($_POST['submit']))
{
$name = $_POST['firstname'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
This is tested code. Similar to what your want.
Change $_SERVER['PHP_SELF'] to #
<?php
error_reporting(E_ERROR );
$first = '';
$last = '';
if (intval($_POST['sub'])){
$first = $_POST['firstname'];
$last = $_POST['lastname'];
}
echo <<<EOT
<html><head></head><body>
<form action="#" method="post">
First name:<br>
<input type='text' name='firstname' value="$first"><br>
Last name:<br>
<input type='text' name='lastname' value="$last"><br><br>
<input type='submit' value='Submit'/>
<input type="hidden" name="sub" value=1/>
</form></body></html>
EOT;
?>
In my first page I have this code:
$number="1234567891";
$str="456";
echo "<form action='edit.php' method='POST'><input type='hidden' name='msg' value='$message' />
<input type='hidden' name='text' value='$number' />
<input type='hidden' name='edit' value='$str' />
<input type='submit' name='chedit' value='Go' style='position:relative; top:25px; left: 50%;'>
</form>";
In my edit.php I have this code:
<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="$mumu"/>
<input type="submit" name="pch" value="Change"/>
</form>
<?php
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
$obinna = str_replace("$change","$mumu","$text");
echo $obinna;
}
}
?>
My problem is that whenever I put a new text in new form and click submit to edit a character in the old string submitted line the page refreshes and no result is output. Please can anybody sort this out?
// try this ..
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
//456 , //555(post value) , //(your text)12345678910
// $obinna = str_replace("Set old value you change in text","set new value you want to set in text ","your orignal text ");
$obinna = str_replace("$mumu","$change","$text");
echo $obinna;
}
}
echo '<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="" placeholder="Change"/>
<input type="text" name="edit" value="" placeholder="Edit"/>
<input type="submit" name="pch" value="Submit"/>
</form>'
Check Demo Url :- https://eval.in/931366
sorry if my querstion is bad, my english is bad
NB : ignore the sql injection filtering
i am using insert() function that I created myself to insert to database
but i am stuck with these code
I want to upload image to ../images directory. If i insert with image included, text is inserted but image not uploaded
i think the query are not going into
if(!empty($gambar))
{
$dir="../images/";
$tmp=$_FILES['gambar']['tmp_name'];
$namafile=time().".jpg";
$ukuran=$_FILES['gambar']['size'];
$file_type=$_FILES['gambar']['type'];
move_uploaded_file($tmp,$dir.$namafile);
$this->perintah="insert into ".$tbl." (".$kol.",gambar) values (".$isi.",".$namafile.")";
$q=mysql_query($this->perintah);
}
but to
else
{
$this->perintah="insert into ".$tbl." (".$kol.") values (".$isi.")";
$q=mysql_query($this->perintah);
}
i have to declare variables
$gambar = $_FILES['gambar'];
and in form tag too
<form method='post' action='controller.php?act=inputsiswa' name='siswa' enctype='multipart/form-data'>
my question:
whats wrong with my code?
at this lines
public function insert($tbl,$kol,$isi,$gambar)
may i declare variable to
public function insert($tbl,$kol,$isi,$gambar = null)
or
public function insert($tbl,$kol,$isi,empty($gambar))
or what?
All of my codes
model.php
public function insert($tbl,$kol,$isi,$gambar)
{
if(!empty($gambar))
{
$dir="../images/";
$tmp=$_FILES['gambar']['tmp_name'];
$namafile=time().".jpg";
$ukuran=$_FILES['gambar']['size'];
$file_type=$_FILES['gambar']['type'];
move_uploaded_file($tmp,$dir.$namafile);
$this->perintah="insert into ".$tbl." (".$kol.",gambar) values (".$isi.",".$namafile.")";
$q=mysql_query($this->perintah);
}
else
{
$this->perintah="insert into ".$tbl." (".$kol.") values (".$isi.")";
$q=mysql_query($this->perintah);
}
echo '<script> alert("Data Berhasil Dimasukkan!"); top.location="index.php?act='.htmlentities($_GET['act']).'";</script>';
if(!$q)
{
echo "<script> alert(\"Gagal Coy !\"); top.location=\"index.php\";</script>";
exit();
}
}
controller.php
case "inputsiswa":
$file = $_FILES['gambar'];
$isi = "'".$_POST['nisn']."','".$_POST['username']."','".$_POST['password']."','".$_POST['nama']."','".$_POST['tempat_lahir']."','".$_POST['tanggal_lahir']."','".$_POST['jenis_kelamin']."','".$_POST['kelas']."','".$_POST['jurusan']."','".$_POST['tipekelas']."','".$_POST['goldar']."','".$_POST['alamat']."','".$_POST['kodepos']."','".$_POST['kontak']."','".$_POST['email']."','Aktif'";
$kol = "nisn,username,password,nama,tempat_lahir,tgl_lahir,jenkel,id_kelas,id_jurusan,id_tipe_kelas,id_goldar,alamat,kode_pos,kontak,email,status";
$as->insert("tbl_siswa",$kol,$isi,$file);
content.php
<form method='post' action='controller.php?act=inputsiswa' name='siswa' enctype='multipart/form-data'>
NISN<br /><input type='text' name='nisn' class='text' required/><br />
Username<br /><input type='text' name='username' class='text' required/><br />
Password<br /><input type='password' name='password' class='text' required/><br />
Ulangi Password<br /><input type='password' name='password2' class='text' required/><br />
Nama Lengkap<br /><input type='text' name='nama' class='text' required/><br />
Tempat Lahir<br /><input type='text' name='tempat_lahir' class='text' required/><br />
Tanggal Lahir <br /><input type='text' name='tanggal_lahir' class='text' required/><br />
Jenis Kelamin <br />
<select name ='jenkel'>
<option value=''>pilih jenis kelamin..</option>
<option value='Pria'>Pria</option>
<option value='Wanita'>Wanita</option>
</select>
<br>
Kelas<br>
<select name='kelas'>
<option value ='0'>pilih kelas..</option>";
$tbl='tbl_kelas';
$isi = $as->select($tbl,'*');
while($r=mysql_fetch_array($isi)){
echo"<option value=$r[id_kelas]>$r[kelas]</option>";
}
echo"</select><br />
Jurusan<br>
<select name='jurusan'>
<option value ='0'>pilih jurusan..</option>";
$tbl='tbl_jurusan';
$isi = $as->select($tbl,'*');
while($r=mysql_fetch_array($isi)){
echo"<option value=$r[id_jurusan]>$r[jurusan]</option>";
}
echo"</select><br />
Tipe Kelas<br>
<select name='tipekelas'>
<option value ='0'>pilih tipe kelas..</option>";
$tbl='tbl_tipe_kelas';
$isi = $as->select($tbl,'*');
while($r=mysql_fetch_array($isi)){
echo"<option value=$r[id_tipe_kelas]>$r[tipe_kelas]</option>";
}
echo"</select><br />
Golongan Darah<br>
<select name='goldar'>
<option value ='0'>pilih golongan darah..</option>";
$tbl='tbl_goldar';
$isi = $as->select($tbl,'*');
while($r=mysql_fetch_array($isi)){
echo"<option value=$r[id_goldar]>$r[nama_goldar]</option>";
}
echo"</select><br />
Alamat<br />
<textarea name='alamat'></textarea><br />
Kode Pos <br /><input type='text' name='kodepos' class='text' /><br />
Kontak <br /><input type='text' name='kontak' class='text' required/><br />
Email <br /><input type='text' name='email' class='text' /><br />
Foto <br /><input type='file' name='gambar' class='text'/><br /><br>
<div style='width:500px; margin-top:-10px;'><input class='graybutton' type='submit' value='Tambahkan'> <input class='graybutton' type='reset' value='Ulangi'></div>
</form>";
In your HTML, you have:
Foto <br /><input type='file' name='gambar' class='text'/><br /><br>
But in your PHP you have:
$file = $_POST['gambar'];
Perhaps I'm missing what you are doing, but surely it should be:
$file = $_FILES['gambar'];
Or am I missing something?
Galih, we've all been noob. But please give us more detail of your problems, instead of "it doesn't work and you're stuck".
Yes, from your codes, the answer from Ralfe is one of the possibilities, and the comment from Kemal Fadillah about query fails is the other one.
From me, if you are doing image upload to the server, make sure the server's directory is writable by the application e.g the PHP.
And be aware from SQL injection since you insert the user's input to MySQL server without any filtering.