-3

This is the code. It is working on localhost, but when I upload it to my server the PHP does not send information to MySQL.

<?php
    session_start();    
    include('connect.php');
        if(isset($_POST['signup'])){
            echo "<pre>"; print_r($_POST); echo "</pre>";
            $name=$_POST['name'];
            $email=$_POST['email'];
            $password=md5($_POST['password']);
           $contact=$_POST['contact'];
            $city=$_POST['city'];
              $address=$_POST['address'];

              $signup_sql=mysqli_query($con,"select * from signup where email='".$email."'");
               //$signup_res=mysqli_query($con,$signup_sql);
               //$signupresult=mysqli_fetch_array($signup_res);

            $dataa = mysqli_fetch_array($signup_sql);

            if(empty($signup_res)){
                   $sql="insert into signup (name,email,password,contact,city,address) values ('".$name."','".$email."','".$password."','".$contact."','".$city."','".$address."')";

                $res=mysqli_query($con,$sql);
                if($res>=1){
                    @header("location:login.php");
                }
                else{
                    @header("location:index.php");
                }
            }else{
                @header("location:index.php");
            } 
        }
    ?>
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 2
    Have you verified that your connection succeeds? It's hard to help without knowing what's in connect.php. Also, you are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. Simply escaping your variables is not enough. See [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). And MD5 hashing is *not* a safe or acceptable way to secure a password. – elixenide Dec 08 '18 at 06:46
  • One more thing: the database is MySQL, not PHPMyAdmin. Your question is about interacting with MySQL; it has nothing to do with PHPMyAdmin, which is just a user interface for MySQL. – elixenide Dec 08 '18 at 06:47
  • You need to show your connect.php to see if you are getting connected to the db. Also your html form would be useful as well to see if the data is being processed correctly – Jonny Dec 08 '18 at 06:59
  • My connect file is – Bhawani Shankar Dec 08 '18 at 07:01
  • @HenryRoy if you are sure of this post the HTML form to see if the data is fine. How do you know the connection is fine? – Jonny Dec 08 '18 at 07:02
  • Sir it is working on localhost but when i host it on 000webhost it is not working – Bhawani Shankar Dec 08 '18 at 07:04
  • @HenryRoy Your problem is in your connection i can tell you from your previous comment. You need a port not localhost to connect to my friend if it is online. Something like localhost:3306 instead of just localhost. You can get this from your server. – Jonny Dec 08 '18 at 07:07
  • Okay i will try – Bhawani Shankar Dec 08 '18 at 07:08
  • But sir for 000webhost it is also localhost not any port – Bhawani Shankar Dec 08 '18 at 07:10
  • And sir login is working – Bhawani Shankar Dec 08 '18 at 07:10
  • @HenryRoy Im sure this will help you the port number will vary from server to server. Your server service provider will tell you what port to connect to. Let me know how it turns out – Jonny Dec 08 '18 at 07:11
  • it will be localhost plus a port number. like localhost:2210 – Jonny Dec 08 '18 at 07:12
  • But by this connect.php login is working already – Bhawani Shankar Dec 08 '18 at 07:13
  • @HenryRoy online it is working? or just on your local machine? Their is a difference on the local machine plain localhost will do it but online a port is needed because you are connecting to a db server. It is set up a bit different. – Jonny Dec 08 '18 at 07:14

3 Answers3

0

You're not testing the select query correctly. if(empty($signup_res)) only tests if the query got an error. But a query that doesn't match anything is not an error, it's just an empty result.

You should check the result of fetching the result:

if(empty($dataa))

And then when you perform the insert query, you're not checking its success correctly, either. $res will be either TRUE or FALSE, not a number. If you want to redirect to login.php if the insert failed, it should be:

if (!$res) {
    header("location: login.php");
} else {
    header("location: index.php");
}

I also recommend that while debugging you remove the redirect and display the error message when the query fails. You also shouldn't use @ while debugging (and there's little reason to use @ for header() calls anyway).

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

YiPp i fixed it by by changing code to

<?php

session_start();
header('location:login.php');

$con = mysqli_connect('localhost','id8127977_decors','Priya@1994');
if($con){
    echo" connection successful";
}else{
    echo " no connection"; 
}

mysqli_select_db($con, 'id8127977_decors');

$name=$_POST['name'];
        $email=$_POST['email'];
        $password=md5($_POST['password']);
       $contact=$_POST['contact'];
        $city=$_POST['city'];
          $address=$_POST['address'];


$q = " select * from signup  where email = '$email' ";

$result = mysqli_query($con, $q);

$num = mysqli_num_rows($result);

if($num == 1){
    echo" duplicate data ";
}else{

    $qy= "insert into signup (name,email,password,contact,city,address) values ('".$name."','".$email."','".$password."','".$contact."','".$city."','".$address."')";
    mysqli_query($con, $qy);
}



?>
0
<!doctype html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>File Upload PHP</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.11.3-jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">

<div class="col-md-8">
<form id="form" action="ajaxupload.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">NAME</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">EMAIL</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required />
</div>

<input id="uploadImage" type="file" accept="image/*" name="image" />
<div id="preview"><img src="filed.png" /></div><br>
<input class="btn btn-success" type="submit" value="Upload">
</form>

<div id="err"></div>
</div>
</div>
</div></body></html>
Codes Dot
  • 1
  • 2