update database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

update database

Post by corillo181 »

come on ppl every topic i post here get like 100 view and 0 answers.. i know someone must know something i ask simple questions..

my new question is..

i got a upload script right?

so the scrib upload pics name and all that kind of pic stuff to a database right? follow along please..

now want i want to do is if that user already got a picture in there when they upload a new one to subtitude the existin data wiht the new one..

i know i can use update * from table where this and that = that..

but

how do i put it on a upload scrip so when the person uplload the picture it does that..

like if(userpicexist delete and update new one){ :-D}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$result = mysql_query("SELECT `img` FROM `table` WHERE `user` = '$user'") or die(mysql_error());
if(mysql_result($result,0) != ''){
  // insert
} else {
  // update
}
:?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: update database

Post by aerodromoi »

corillo181 wrote:so the scrib upload pics name and all that kind of pic stuff to a database right? follow along please..
This is not exactly a precise definition... :roll:
If you know how to UPDATE a table, you'll certainly know how to use the SELECT command.

Code: Select all

$query = "SELECT * FROM usertable WHERE username like '%$postedname%' ORDER BY id DESC";
If the user is already in the database, you'll just have to check whether he's/she's already uploaded something.

aerodromoi
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: update database

Post by timvw »

corillo181 wrote: i know i can use update * from table where this and that = that..
I know that you can't with the syntax you posted.
I do know that the syntax for an update statement is described in http://dev.mysql.com/doc/refman/5.0/en/update.html.
corillo181 wrote:come on ppl every topic i post here get like 100 view and 0 answers.. i know someone must know something i ask simple questions..

my new question is..
like if(userpicexist delete and update new one){ :-D}
We're not here to do *your* homework...

http://be2.php.net/manual/en/language.c ... ctures.php
http://dev.mysql.com/doc/refman/5.0/en/insert.html
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

thanx to the first guy who posted here...thats all i needed..


and to the last guy i'm not telling you to do my home work.. a few guys just give me small clues and i can fallow up from there.. is just guys like you that think everyone should be born knowing that try to post something smart all the time..
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

this si the bes ti could come up with but it does not work,,

Code: Select all

<?php
session_start();
include 'db.php';
$dir="userpic/";
$actuser=$_SESSION['username'];

if(isset($_POST['upload']))
{
$checkpic="SELECT username FROM userpic  LIMIT 1";
$query7=mysql_query($checkpic);
while($checkuser=mysql_fetch_array($query7)){
if($checkuser > 0){
$query00=mysql_query("UPDATE userpic WHERE username='$actuser'");
if(!query00){ echo"error";
}
}else{


if ($_FILES['ulfile']['type']!="image/pjpeg") {echo"that kind of file is not permitted please only use jpg images.";
exit();
}
if($_FILES['ulfile']['name']==""){echo"at least pic a picture man!";
}
$filename = $_FILES['ulfile']['name'];
$tmpname = $_FILES['ulfile']['tmp_name'];
$filesize = $_FILES['ulfile']['size'];
$filetype = $_FILES['ulfile']['type'];
$datetime = date('m-d-y h:i:s');

$filepath = $dir . $filename;
$result=copy($tmpname, $filepath);
if(!$result){ echo " a problem occur";
}
$sql2=mysql_query("INSERT INTO userpic (name,username,datetime, size, type, path)VALUES('$filename','$actuser','$datetime','$filesize', '$filetype', '$filepath')") or die(mysql_error());
header('Location: testphp/');
}
}
}
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$query00=mysql_query("UPDATE userpic WHERE username='$actuser'");
You're not updating anything with that statement.

A standard UPDATE would look like:

Code: Select all

UPDATE userpic SET foo = '$bar' WHERE username = '$actuser'
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Also, this line

Code: Select all

if ($_FILES['ulfile']['type']!="image/pjpeg")
should be

Code: Select all

if(($_FILES['ulfile']['type'] != "image/pjpeg") && ($_FILES['ulfile']['type'] != "image/jpeg"))
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

now what is wrong here?
it works fine but the update still does not work..

Code: Select all

$checkpic=mysql_query("SELECT * FROM userpic where username='$actuser' LIMIT 1")or die(mysql_error());
if($check > 0){
$update=mysql_query("UPDATE userpic  SET  name='$filename',username='$actuser',datetime='$datetime', size='$filesize', type='$filetype', path='$filepath' WHERE username='$actuser'");
}elseif($check<=0){
$sql2=mysql_query("INSERT INTO userpic (name,username,datetime, size, type, path)VALUES('$filename','$actuser','$datetime','$filesize', '$filetype', '$filepath')");
header('Location: /testphp/');
}
}
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Translate this....

If user uploads new pic
query database and get name of old pic
if new pic is ok delete old pic
create new pic
update database with new pic


Granted this is raw but it's not that hard. You insulted someone who tried to help you so I would encourage you to RTFM
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i never insulted any one, i just thnk if you going to take your time to help someone make sure you do it right or that you want to help that person out..

and the only problem with that script was that i forget the fetch array..

thank you..
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

100 views and no replies could be due to the lack of indentation in your code.

I like to help people, but I don't have hours and hours of free time, so if code isn't indented, I'll just hit the back button.
Post Reply