2

without image form submitting successfully but not works with image. also changed the name of image from k.jpg to 37732-k.jpg when uploaded
when i click on uploaded image name so its show number and "2b2b2 " somthing like that. plz help

****Here is complete php code .**

<?php
include('../dbcon.php');
if (isset($_POST['submit'])) {

    $roll_no=$_POST['roll_no'];
    $name=$_POST['name'];
    $city=$_POST['city'];
    $std=$_POST['std'];
    $p_cont=$_POST['p_cont'];

    // $picture_type=$_FILES['image']['type'];
    // $imagename=$_FILES['image']['name'];
    // $tempname=$_FILES['image']['tmp_name'];


    // move_uploaded_file($tempname,"../dataimages/$imagename/");


$pic = rand(1000,100000)."-".$_FILES['image']['name'];
        $pic_loc = $_FILES['image']['tmp_name'];
        $folder="../dataimages/";
        if(move_uploaded_file($pic_loc,$folder.$pic))
        {
            ?><script>alert('successfully uploaded');</script><?php
        }
        else
        {
            ?><script>alert('error while uploading file');</script><?php
        } 


 $query="INSERT INTO `student`( `roll_no`, `name`, `city`, `p_cont`, `standerd`,'image') VALUES ('$roll_no','$name','$city','$std','$p_cont','$pic')";
$run=mysqli_query($con,$query);


if ($run == true) {

    ?>
    <script>
    alert('data inserted successfully');
    window.open('add_student.php','self');



    </script>

    <?php
} else{

?>
    <script>
    alert('data insertion failed');
    window.open('add_student.php','self');



    </script>
  <?php
}
}


?>
Fazal
  • 35
  • 3
  • Have no idea what you mean though – Rotimi Oct 20 '17 at 16:34
  • Do you get any error/alert message ? – Ravinder Reddy Oct 20 '17 at 16:38
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 20 '17 at 16:38
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Oct 20 '17 at 16:38
  • [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](https://phpdelusions.net/pdo/mysqli_comparison) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Oct 20 '17 at 16:38
  • with out image file form inserting but when i add when add file code its not working even not showing any erro Akintunder. – Fazal Oct 20 '17 at 16:39
  • did not get any error but when image uploaded to folder the name of the image change from ( k.jpg ) to (37732-k.jpg) . and not storing in db. – Fazal Oct 20 '17 at 16:41
  • you are using rand() function before image name thats why your uploaded file name get changed. – Subhash Shipu Oct 20 '17 at 16:44
  • Please confirm: move_uploaded_file works, mysqli_query does not. Right? Please print the value of $query before you run mysqli_query and post it here. – Nic3500 Oct 20 '17 at 17:13

0 Answers0