Connecting to a database $conn = mysql_connect ("localhost", "phpdb", "phpdb"); mysql_select_db ("Test", $conn); ?> ? Get the parameters of a Web page $id =$_post[' id ']; Determine if the user name already exists $checksql = "SELECT * from image where id= ' $id '"; $check _re=mysql_query ($checksql, $conn); $num =mysql_num_rows ($check _re); if ($num!=0) { echo "<table width= ' 100% ' align=center><tr><td align=center>"; echo "This username already exists, please select another <br>"; echo "<font color=red> upload failed! </font><br><a href= ' web effects: history.back ( -1) ' > Return </a> '; echo "</td></tr></table>"; Exit (); } Method Two: Save only the file name, When you save the file name, the file is in the upload temporary directory set in the php.ini configuration file, which is the Upload_tmp_dir parameter if ($photo <> "") { if ($photo _type== "Image/pjpeg") or ($photo _type = = "Image/gif")) { C:WINNT EMP Enables temporary directory of upload files set in php.ini configuration file $photodir = "C:winnt emp/"; if (!) ( File_exists ($photo _name))) { Copy the picture file to the Set upload file temp directory Copy ($photo, $photodir. $photo _name); } } else{ echo "<script Language=javascript>alert (' uploading photos can only be jpg or gif! '); History.go ( -1) </script><br> "; echo "or <br>"; echo "FileName already exists, please change a filename for the picture"; Exit } } else{ $photo _name= ""; } $sql = "INSERT into image (ID, photo) VALUES (' $id ', ' $photo _name ')"; mysql_query ($sql, $conn) or Die ("Insert data failed:". Mysql_error ()); Close connection Mysql_close ($conn); Show upload Image Successful Redirect to Registration Success page Header ("location:display_image2.php?id= $id"); ?> Code two <?php Connecting to a database $conn = mysql_connect ("localhost", "phpdb", "phpdb"); mysql_select_db ("Test", $conn); ?> ? Get the parameters of a Web page $id =$_post[' id ']; //To determine if the user name already exists $checksql = "SELECT * from image where id= ' $id '; $check _re=mysql_query ($checksql, $conn); $num =mysql_num_rows ($check _re); if ($num!=0) { echo "<table width= ' 100% ' align=center><tr><td align=center>"; echo "This username already exists, please select another <br>"; echo "<font color=red> upload failed! </font><br><a href= ' Javascript:history.back ( -1) ' > Return </a> '; echo "</td></tr></table>"; exit (); } //Method One: Save the picture file in MySQL, //If there is a picture file, open the picture file, the data in the picture file with the function //addslashes processing, and then pass to the variable $data, // The Addslashes function is to add a slash to the string so that the string can be successfully written to the database //The $data of the picture file is saved in the variable, if ($photo <> "") { $fp =fopen ( $photo, "R"); $data =addslashes (fread ($fp, FileSize ($photo)); } $password =md5 ($password); $sql = INSERT INTO image (Id,photo) VALUES (' $id ', ' $data '); mysql_query ($sql, $conn) or Die ("Insert data failed:". Mysql_error ()); Close connection Mysql_close ($conn); Show upload Image Successful Redirect to Registration Success page Header ("location:display_image1.php?id= $id"); |