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));
?>
Related
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()
I'm doing project and i want to update user profile using php mysql when i click
on update button it show that there are error in line 17. please help to find the error.
<?php
session_start();
if(!isset($_SESSION["n"]))
{
header("location:error.php");
}
if(isset($_POST["s"]))
{
$name=$_POST["nm"];
$lname=$_POST["lnm"];
$address=$_POST["ad"];
$u=$_SESSION["un"];
$query = "SELECT * FROM signup";
$result=mysqli_query($result,$query) or die(mysqli_connect_error());
$i=0;
while($row=mysqli_fetch_array($result))
{
$roll[$i]=$row['rollno'];
$i++;
}
$total_elmt=count($roll);
require_once("vars.php");
$conn=mysqli_connect(host,uname,pass,db) or die(mysqli_connect_error());
$query="update signup set name='$nm',lname='$lnm',address='$ad' where user_id='$value'";
$execute=mysqli_query($conn,$query);$r=mysql_affected_rows();
mysqli_close($conn);
$msg="Your information is submitted successfully";
}
?>
you need to open the database first to run the select query.
move this line to the top:
$conn=mysqli_connect(host,uname,pass,db) or die(mysqli_connect_error());
you are using mysqli_query function wrong. it should be
$result=mysqli_query($conn,$query) or die(mysqli_connect_error());
Now i have updated your code
<?php
session_start();
if(!isset($_SESSION["n"]))
{
header("location:error.php");
}
if(isset($_POST["s"]))
{
$name = $_POST["nm"];
$lname = $_POST["lnm"];
$address = $_POST["ad"];
$u = $_SESSION["un"];
$conn = mysqli_connect(host,uname,pass,db) or die(mysqli_connect_error());
$query = "SELECT * FROM signup";
$result = mysqli_query($conn, $query);
$i=0;
while($row=mysqli_fetch_array($result))
{
$roll[$i]=$row['rollno'];
$i++;
}
$total_elmt=count($roll);
require_once("vars.php");
$query="update signup set name='$nm',lname='$lnm',address='$ad' where user_id='$value'";
$execute=mysqli_query($conn,$query);
$r=mysql_affected_rows();
mysqli_close($conn);
$msg="Your information is submitted successfully";
}
?>
I have 2 tables. the first is customers the second is invoice, both have a row for "customer ID". When adding a new invoice through a web form how do I make it so it will take the "customer ID" from customers and put it into the "customer ID" in invoice along with the new information upon submission of the form?
This is the code from my index.php
?php
include 'DBConfig.php';
//Insert or Update contact information
if(isset($_POST['action_type']))
{
if ($_POST['action_type'] == 'add' or $_POST['action_type'] == 'edit')
{
//Sanitize the data and assign to variables
$invum = mysqli_real_escape_string($link, strip_tags($_POST['invnum']));
$cost = mysqli_real_escape_string($link, strip_tags($_POST['cost']));
if ($_POST['action_type'] == 'add')
{
$sql = "insert into invoice set
cost = '$cost'";
}else{
$sql = "update invoice set
cost = '$cost',
where invnum = $invnum";
}
if (!mysqli_query($link, $sql))
{
echo 'Error Saving Data. ' . mysqli_error($link);
exit();
}
}
header('Location: invoicelist.php');
exit();
}
//End Insert or Update contact information
//Start of edit contact read
$gresult = ''; //declare global variable
if(isset($_POST["action"]) and $_POST["action"]=="edit"){
$id = (isset($_POST["ci"])? $_POST["ci"] : '');
$sql = "select invnum, cost from invoice
where invnum = $id";
$result = mysqli_query($link, $sql);
if(!$result)
{
echo mysqli_error($link);
exit();
}
$gresult = mysqli_fetch_array($result);
include 'invoiceupdate.php';
exit();
}
//end of edit contact read
I have this code :
if(isset($_POST['remove'])){
$con = mysqli_connect(".","","","");
$q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");
header( "refresh:2;url=settings.php" );
echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
}
It is working, but I want it to do something like this
if(isset($_POST['remove'])){
$con = mysqli_connect("","","","");
while($row = mysqli_fetch_assoc($q)){
if($row['picture'] == ""){
echo "<div class='notemarg'> No pictures to delete</div>";
} else {
$q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");
header( "refresh:2;url=settings.php" );
echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
}
}
}
This means that the picture from database will be removed only if there IS any picture.. if not, then it will display that message "No pictures to delete" ... but it does not work.. it still shows that error message that there is no picture even though there is no blank row in database and so it does not delete the information in row either...
Where is problem?
BTW: first code works fine... and it works even if there is nothing in database so it kinda does not make sense that the "profile picture has been removed." is being displayed...
Try this instead:
$con = mysqli_connect("","","","");
if(isset($_POST['remove'])){
$q = mysqli_query($con, "SELECT picutre FROM members where username = '". $_SESSION['username']. "'");
$row = mysqli_fetch_assoc($q);
if( empty($row['picture'])){
echo "<div class='notemarg'> No pictures to delete</div>";
}
else {
$q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");
header( "refresh:2;url=settings.php" );
echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
}
}
You need the mysqli_query statement:
$con = mysqli_connect("","","","");
if(isset($_POST['remove'])){
$q = mysqli_query($con,"SELECT IFNULL(picture,'') AS picture
FROM members
WHERE username = '".$_SESSION['username']."'");
$row = mysqli_fetch_assoc($q);
if( empty($row['picture'])){
echo "<div class='notemarg'> No pictures to delete</div>";
}
else {
$q = mysqli_query($con,"UPDATE members SET picture = '' WHERE username = '".$_SESSION['username']."'");
header( "refresh:2;url=settings.php" );
echo "<div class='notemarg'>Profile Picture has been removed. Refreshing page within 3 seconds...</div>";
}
}
table name -- breaking_news
field name -- status
I have created a single page and passes id from link click.
Now I check if status not empty and status equal to Inactive
then update status = Active Else update status Inactive
but it is not working properly.
It Only Works For If Condition.
The ELSE Condition of Code is Not Working . plz suggest me how to write in if else properly...
<td><img src="img/active.png" width="24" height="24" border="0" title="Active" /></td>
<?php
$Admin = new admins;
$sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(!empty($row) && $row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
?>
Try this code
">
<?php
$Admin = new admins;
$sql = "SELECT status FROM breaking_news WHERE id=".mysql_real_escape_string($_GET['status_active']);
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(!$row && $row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
?>
you have just to ask if $row was have more or not, be cause mysql_fetch_assocreturn false if it's empty look to this manual : mysql_fetch_assoc PHP
you have just do not use empty()
Or like that :
if($row = mysql_fetch_assoc($result))
{
if($row['status']=='Inactive')
{
mysql_query("Update breaking_news SET status='Active' WHERE id=".mysql_real_escape_string($_GET['status_active']));
$_SESSION['message'] = "Status Activated Successfully";
header("Location:breaking_news.php");
exit;
}
}
else
{
mysql_query("Update breaking_news SET status='Inactive' WHERE id=".mysql_real_escape_string($_GET['status_inactive']));
$_SESSION['message'] = "Status De-Activated Successfully";
header("Location:breaking_news.php");
exit;
}
If you have not found a row, there is nothing to update...
You may wish to do something like this:
<?php
if (!empty($row)) {
if ($row['status'] == 'Inactive') {
//update to active
}
else if ($row['status'] == 'Active') {
//update to inactive
}
}
also you've got a typo in the second call to $_GET array: $_GET['status_inactive']. You should be updating the same row but with different status value.
EDIT
#toto21 I don't have enough reputation to comment on your answers but no, your answer is wrong. As I mentioned at the top - if there is no row fetched then there is nothing in the db for you to update, so your else statement makes no sense.