Page 1 of 1
update database
Posted: Sun May 07, 2006 4:37 am
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){

}
Posted: Sun May 07, 2006 6:02 am
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
}

Re: update database
Posted: Sun May 07, 2006 6:05 am
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...
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
Re: update database
Posted: Sun May 07, 2006 6:11 am
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){

}
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
Posted: Sun May 07, 2006 1:26 pm
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..
Posted: Sun May 07, 2006 2:52 pm
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/');
}
}
}
?>
Posted: Sun May 07, 2006 2:55 pm
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'
Posted: Sun May 07, 2006 2:57 pm
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"))
Posted: Sun May 07, 2006 6:31 pm
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/');
}
}
?>
Posted: Sun May 07, 2006 6:47 pm
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
Posted: Sun May 07, 2006 8:55 pm
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..
Posted: Sun May 07, 2006 9:09 pm
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.