WeLoveCSS

WeLoveCSS (http://welovecss.com/index.php)
-   Scripting and Server Side (http://welovecss.com/forumdisplay.php?f=7)
-   -   How can I upload image with other data from my form? (http://welovecss.com/showthread.php?t=9500)

software_ss 13th February 16 02:05 PM

How can I upload image with other data from my form?
 
http://i67.tinypic.com/345c45d.jpg

I am learning php mysql. I have a form like the image :
This form is working but unable to upload image. Several code is trying but failed.
My table name is ‘student’ and column name is ‘std_img’. Here is the form page, name ‘addstud.php’ code:
<?php
function createRandomPassword() {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {

$num = rand() % 33;

$tmp = substr($chars, $num, 1);

$pass = $pass . $tmp;

$i++;

}
return $pass;
}
$finalcode='SID-'.createRandomPassword();
?>
<style type="text/css">
<!--
.ed{
border-style:solid;
border-width:thin;
border-color:#00CCFF;
padding:5px;
margin-bottom: 4px;
}
#button1{
text-align:center;
font-family:Arial, Helvetica, sans-serif;
border-style:solid;
border-width:thin;
border-color:#00CCFF;
padding:5px;
background-color:#00CCFF;
height: 34px;
}
-->
</style>
<form name="myForm" action="addstudexec.php" method="post" enctype="multipart/form-data" name="addroom" onsubmit="return validateForm()">
<br />
<table width="596" border="0">
<tr>
<td width="125">Image of the student</td>
<td width="455">

<!-- image upload test code

<input type="file" name="std_img" required style="width:180px">




<!-- image upload test code -- end-->


</td>
</tr>
<tr>
<td>Id Number</td>
<td><input name="id" type="text" class="ed" id="brnu" size="50" /></td>
</tr>
<tr>
<td>Student's Name</td>
<td><input name="std_name" type="text" class="ed" id="brnu" size="50" /></td>
</tr>
<tr>
<td>Father's Name</td>
<td><input name="std_fname" type="text" class="ed" id="brnu" size="50" /></td>
</tr>
<tr>
<td>Mother's Name</td>
<td><input name="std_mname" type="text" class="ed" id="brnu" size="50" /></td>
</tr>
<tr>
<td>Address</td>
<td><input name="std_address" type="text" class="ed" id="brnu" size="70" /></td>
</tr>
<tr>
<td>Date Of Birth</td>
<td><input name="std_dob" type="text" class="ed" id="brnu" size="50" /></td>
</tr>
<tr>
<td>Gender</td>
<td>
<table width="200" border="0">
<tr>
<td width="100"><select name="std_gender" class="ed">
<option>Male</option>
<option>Female</option>
<option>Other</option>
</select></td>
<td width="84">Religion</td>
<td width="84"><select name="std_religion" class="ed">
<option>Islam</option>
<option>Hinduism</option>
<option>Christianity</option>
<option>Buddhism</option>
<option>Other</option>
</select></td>
<td>Nationality</td>
<td> <select name="std_nationality" class="ed">
<option>Bangladeshi</option>
<option>Other</option>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>


</tr>
<tr>
<td>&nbsp;</td>
<td> Official Informations</td>
</tr>
<tr>
<td>Technology</td>
<td><table width="423" border="0">
<tr>
<td width="180"><select name="std_department" class="ed">
<option>Electrical Technology</option>
<option>Civil Technology</option>
<option>Mechanical Technology</option>
</select></td>
<td width="102">Admission Date:</td>
<td width="57"><input name="do_admission" type="text" class="ed" id="brnu" size="15" /></td>
</tr>
</table></td>
</tr>
<tr>
<td>Student's Cell No.</td>
<td><table width="455" border="0">
<tr>
<td width="188"><input name="std_cellno" type="text" class="ed" id="brnu" size="20"/></td>
<td width="107">Father's Cell No.</td>
<td width="146"><input name="std_fcellno" type="text" class="ed" id="brnu" size="20" /></td>
</tr>
</table>
</td>

</tr>
<tr>
<td>Student's Login Info:</td>
<td><table width="449" border="0">
<tr>
<td width="74">Username:</td>
<td width="94"><input name="std_username" type="text" class="ed" id="brnu" size="15" /> </td>
<td width="69">Password</td>
<td width="176"><input name="std_password" type="text" class="ed" id="brnu" size="15" /></td>
</tr>
</table></td>
</tr>
<tr>

</tr>
</table>

<br>
<input type="submit" name="Submit" value="save" id="button1" />
</form>


And ‘addstudexec.php’ code is:
<?php
include('../connect.php');

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str)
{
$str = @trim($str);
if(get_magic_quotes_gpc())
{
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$id = clean($_POST['id']);
$std_name = clean($_POST['std_name']);
$std_fname = clean($_POST['std_fname']);
$std_mname = clean($_POST['std_mname']);
$std_address = clean($_POST['std_address']);
$std_dob = clean($_POST['std_dob']);
$std_gender = clean($_POST['std_gender']);
$std_religion = clean($_POST['std_religion']);
$std_nationality = clean($_POST['std_nationality']);
$std_department = clean($_POST['std_department']);
$do_admission = clean($_POST['do_admission']);

$std_cellno = clean($_POST['std_cellno']);
$std_fcellno = clean($_POST['std_fcellno']);
$std_username = clean($_POST['std_username']);
$std_password = clean($_POST['std_password']);

//<!-- start of phptoupload test code -->




// End of photoupload test code



mysql_query("INSERT INTO student (id,std_name,std_fname,std_mname,std_address,std_d ob,std_gender,std_religion,std_nationality,std_dep artment,do_admission,std_cellno,std_fcellno,std_us ername,std_password)
VALUES ('$id','$std_name','$std_fname','$std_mname','$std _address','$std_dob','$std_gender','$std_religion' ,'$std_nationality','$std_department','$do_admissi on','$std_cellno','$std_fcellno','$std_username',' $std_password')");
mysql_query("INSERT INTO user (id, std_username, std_password)
VALUES ('$id','$std_username','std_password')");
header("location: student.php");
?>

meesa 13th February 16 05:00 PM

Re: How can I upload image with other data from my form?
 
Check out this tutorial on how to upload the file:

https://davidwalsh.name/basic-file-uploading-php

Then, don't try to put the image itself into the database. Save it in a folder outside of the main root (where index.html is) and put only the path to it in the database.

Also, you should look into use PDO or SQLite, as the MySQL_* methods are depreciated in PHP now.


All times are GMT. The time now is 04:08 AM.

Powered by vBulletin Version 3.8.4 Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.