I need your help guys. My code updates only the first image. When I try to update two more images my code doesn't work.
This my code
<?php
include ("config.php");
$id= $_POST['id'];
$judul = $_POST['judul'];
$tanggal = $_POST['tanggal'];
$konten1 = $_POST['konten1'];
$konten2 = $_POST['konten2'];
$konten3 = $_POST['konten3'];
$posisi=$_POST['posisi'];
$posisi2=$_POST['posisi2'];
$tema=$_POST['tema'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
$lokasi_foto1 = $_FILES['foto1']['tmp_name'];
$nama_foto1 = $_FILES['foto1']['name'];
$lokasi_foto2 = $_FILES['foto2']['tmp_name'];
$nama_foto2 = $_FILES['foto2']['name'];
$lokasi_logo = $_FILES['logo']['tmp_name'];
$nama_logo = $_FILES['logo']['name'];
if(isset($_FILES['logo'] ) && ($_FILES['banner']) && $_FILES['banner']['name'] !="" && $_FILES['logo']['name'] !="") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['logo'];
$e = 'upload/'.$r['banner'];
unlink ($d);
unlink ($e);
move_uploaded_file($lokasi_logo,"upload/".$nama_logo);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file' , judul='$judul', tanggal='$tanggal', posisi='$posisi',konten1='$konten1', konten2='$konten2', konten3='$konten3', tema='$tema' ,posisi2 = '$posisi2', footer='$footer' , logo='$nama_logo' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
}
if ($edit = mysql_query("UPDATE newsletter SET judul='$judul', tanggal='$tanggal', posisi='$posisi',konten1='$konten1', konten2='$konten2', konten3='$konten3' , tema='$tema' ,posisi2 = '$posisi2', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
So if I try to update the banner and the logo, my code just updates the banner. The Logo is not updated. If anyone can help , I will be happy :)
EDIT :
The problem on this my code is on after if (isset) I am confuse how to use multiple isset with use that
You're using exit(), meaning that when the banner is uploaded, the script will terminate. Unless there's more code to this, there's no need to use exit()
Source: PHP exit()
Related
I have Code to Create/Update/Delete my data from database.
I have data and a banner image. I do not want to update the image if only the data is set.
So, I try to make a code validator: if I update only data -not the image- my image is not set to null.
But my validation does not detect if I have filled my input file or not...
This my code:
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner'])) {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
}if (empty($lokasi_file)) {
if ($edit = mysql_query("UPDATE newsletter SET konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file', konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
I am so happy if anyone can help me. i am try to update image with name banner
Issue is in your conditions and in last query. i think in last update you dont need to update image. change your code as below
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner']) && $_FILES['banner']['name'] != "") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
}
if (!empty($lokasi_file)) { //change here need to check "not empty" instead of "empty"
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file',konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
//Remove banner from below query
if ($edit = mysql_query("UPDATE newsletter SET , konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
EDIT
Change your logic as below if banner is uploaded then move and update banner otherwise only update other data
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner']) && $_FILES['banner']['name'] != "") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file',konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
//Remove banner from below query
if ($edit = mysql_query("UPDATE newsletter SET , konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
I've tried to use ob_start(); and exit(); ,both are not working, please advise, thank you very much
<?php
ob_start();
// connect to the database
include('connect-db.php');
// check if the form has been submitted.
// If it has, start to process the form and save it to the database
// once saved, redirect back to the view page
if (isset($_POST["submit"]))
{
foreach ($_POST['patientid'] as $index => $patientid)
{
$id = mysql_real_escape_string($_POST['id'][$index]);
$data1 = mysql_real_escape_string($patientid);
$data2 = mysql_real_escape_string($_POST['vaccineid'][$index]);
$data3 = mysql_real_escape_string($_POST['vaccinename1'][$index]);
$data4 = mysql_real_escape_string($_POST['vaccinename2'][$index]);
$data5 = mysql_real_escape_string($_POST['vaccinename3'][$index]);
$data6 = mysql_real_escape_string($_POST['totalnoofinjection'][$index]);
$data7 = mysql_real_escape_string($_POST['nthinjection'][$index]);
$data8 = mysql_real_escape_string($_POST['date'][$index]);
$data9 = mysql_real_escape_string($_POST['nextdate'][$index]);
$data10 = mysql_real_escape_string($_POST['skip'][$index]);
$data11 = mysql_real_escape_string($_POST['language'][$index]);
mysql_query("UPDATE patientvaccinedetail SET patientid = '$data1',
vaccineid = '$data2', vaccinename1 = '$data3',
vaccinename2 = '$data4', vaccinename3 = '$data5',
totalnoofinjection = '$data6', nthinjection = '$data7',
date = '$data8', nextdate = '$data9', skip = '$data10',
language = '$data11'
WHERE id=$id") or die(mysql_error());
header("Location: start.php");
exit;
}
}
Just updated and still cant't redirect to another pages
You are missing semi colon after exit
Corrected code:
exit;
use ob_end_clean(); before the header call and use exit; instead of exit
try like below
ob_end_clean();
header("Location: start.php");
exit;
<?php
if(isset($_POST['confirm'])){
$name = $db->validation($_POST['name']);
$id = "";
for($i= strlen($name) ; $i>=1 ; $i--){
if($name[$i-1] == 'a'){
break;
}else{
$id = $id.$name[$i-1];
}
}
$userid = strrev($id);
$query = "SELECT * FROM bluekolar_user WHERE token = '$name' AND user_id='$userid'";
$result = $db->select($query);
if($result){
if(mysqli_num_rows($result) ==1 ){
$qu = "UPDATE bluekolar_user SET active = '1' WHERE user_id='$userid'";
$res = $db->update($qu);
if($res){
$url = "profile/index.php?id=".$userid ;
header('location:'.$url) ;
exit();
}else{
$error = "Provide correct confirmation code";
}
}else{
$error = "Provide correct confirmation code";
}
}
}
?>
The code is performing good but it is unable to redirect. I don't know why. I need help.
Please help me so that I can redirect my code command to another page.
Thank you.
You need to ensure the path you are trying to redirect to is correct. The code in your question will try to redirect to a "profile" subdirectory of the location where this script is running. If you want to go to "http://example.com/profile/index.php?id=" you need to change your $url to be "/profile/index.php"
This code runs when a user hits a delete button on my form. I am trying to copy a file, $picfile, from "/pics/" to "/pics/deletedrecordpics/" and then delete the orginal. Finally, delete the record from the database. Deleting the record from the databse works, but copying the file and deleting the original does nothing. There are no errors in the error log, so I am really confused as to why this code isn't running as I think it should.
if ($allowdelete==true && $thepassword == $password)
{
//delete record that delete was set to by button
//$sql = ("DELETE FROM $table WHERE id=$id");
$sql = ("select picfile,title,author from $table where id=$delete");
$file=mysql_query($sql);
$resrow = mysql_fetch_row($file);
$picfile = $resrow[0];
$title = $resrow[1];
$author = $resrow[2];
if (file_exists("/pics".$picfile)){
copy("/pics/".$picfile,"/pics/deletedrecordpics/".$author."-".$title."-".$picfile);
unlink("/pics/".$picfile);
echo $available = "image is available.";
$sql = ("DELETE FROM $table WHERE id=$delete");
$result = mysql_query($sql);
if ($result){
echo "Your Picture has been removed from our system.";
Die($available);
}
else{
echo "There was an error in removing your picture.";
$Delete = "";
Die();
}
}
else{
echo $available = "image is not available.";
}
}
The weird part is a have almost the same code in a delete button on my control panel located in "/adminpanel" and it works perfectly. The code for that is the same except I use $id instead $delete and "../" before all the "pics/" because it's in the adminpanel folder. The permissions are right and the folder exists because the code works with that page. And I know $delete is getting set because the record gets deleted from the database. I know picfile, author and title are getting set because I appended them to the print statement and they were all right. Really confused. Any ideas?
Here is the code for the working page
q = ("select picfile,title,author from $table where id=$id");
$file=mysql_query($q);
$resrow = mysql_fetch_row($file);
$picfile = $resrow[0];
$title = $resrow[1];
$author = $resrow[2];
copy("../pics/".$picfile,"../pics/deletedrecordpics/".$author." - ".$title." - ".$picfile);
unlink("../pics/".$picfile);
$file=mysql_query($q);
$q = ("DELETE FROM $table WHERE id=$id");
$file=mysql_query($q);
why is this line repeated twice $file=mysql_query($q); ?
Try this
$file_path = $_SERVER["DOCUMENT_ROOT"]."/pics/";
if (file_exists($file_path.$picfile))
{
copy($file_path.$picfile, $file_path."/deletedrecordpics/".$author."-".$title."-".$picfile);
unlink($file_path.$picfile);
}
else
{
echo "File not found!!!!!!!!";
}
I used to store all my data in 000webhost, today I decided to move to hostinger. So.. after moving it I replaced the old mysql_connect info by the new one. Alright, after doing that I tested it, everything has ran fine, except some echo functions.
check file (connects to the server and do the login):
<?php
$servidorr = "mysql.XXXX.co.uk";
$bdd = "XXXXXXXX";
$usuarioo = "XXXXX";
$senhaa = "XXXXXXX";
if (!empty($_POST) AND (empty($_POST['usuario']) OR empty($_POST['senha']))) {
header("Location: geton"); exit;
}
mysql_connect($servidorr, $usuarioo, $senhaa) or trigger_error(mysql_error());
mysql_select_db($bdd) or trigger_error(mysql_error());
$usuario = mysql_real_escape_string($_POST['usuario']);
$senha = mysql_real_escape_string($_POST['senha']);
$lang = mysql_real_escape_string($_POST['lang']);
$sql = "SELECT `id`, `nome`, `nivel` FROM `usuarios` WHERE (`usuario` = '". $usuario ."') AND (`senha` = '". sha1($senha) ."') AND (`ativo` = 1) LIMIT 1";
$updatelang = "UPDATE usuarios SET lang='$lang' WHERE usuario='$usuario'";
$query = mysql_query($sql);
if (mysql_num_rows($query) != 1) {
echo "<script>alert('Oops! Looks like there is something wrong with your login! *perhaps a typo or you did not fill out the fields*'); location.href='geton'</script>"; exit;
} else {
$resultado = mysql_fetch_assoc($query);
mysql_query($updatelang);
if (!isset($_SESSION)) session_start();
$_SESSION['UsuarioID'] = $resultado['id'];
$_SESSION['UsuarioNome'] = $resultado['nome'];
$_SESSION['usuario'] = $resultado['usuario'];
$_SESSION['UsuarioNivel'] = $resultado['nivel'];
$_SESSION['lang'] = $resultado['lang'];
header("Location: http://mapmaking.zz.mu/pages/home"); exit;
}
?>
Home file (these echos are just for testing and this is not the original file, the original one has the same php stuff, except the echo functions, those are in random lines):
<?php
if (!isset($_SESSION)) session_start();
$tlang = $_SESSION['UsuarioLang'];
$aclevel = $_SESSION['UsuarioNivel'];
$nick = $_SESSION['UsuarioNome'];
$neededal = 1;
if (!isset($_SESSION['UsuarioID']) OR ($_SESSION['UsuarioNivel'] < $neededal)) {
session_destroy();
header("Location: http://inside.mapmaking.uk.to/geton"); exit;
}
session_start();
echo $tlang;
echo $aclevel;
echo $nick;
echo "$level$tlang$tlang";
?>
[this one basically start the session and check if the connected user acess level is 1]
Echo $tlang does not work! :( somehow it doesn’t, I have no idea why ;_;
Hope you guys help me, thank you!!
$_SESSION['lang'] != $_SESSION['UsuarioLang']
You assign a value to the first one, yet expect value from the second one.
$_SESSION['lang'] = $resultado['lang'];
$tlang = $_SESSION['UsuarioLang'];
Change this line:
$_SESSION['lang'] = $resultado['lang'];
to the following:
$_SESSION['UsuarioLang'] = $resultado['lang'];
You should also call session_start() without the isset check. Also, you should consider using && instead of AND and || instead of OR, as PHP has weird operator precedence rules (the assignment = has a higher precendence than either AND or OR).