Error entering data in MySQL

Codes here !

Moderators: gesf, Michalio

Post Reply
php4fun
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Apr 16, 2022 4:49 am

I have created User Registration page which is part of a database application.I have downloaded an application from net tried to customize according to my need. I am a new learner of PHP MySQL application.Before doing this I just prepared a simple CRUD application by watching Youtube videos.Now in my User Registration page data is not going to the MySQL database.I have tried many ways with my little knowledge with almost no success. Can anyone help me where the code is wrong?
The code is as follows :

Code: Select all

[b]std_register.php[/b]

<html><head>
<title>Student Registration </title>
<link href="css/student_styles.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="js/user.js">
</script>
</head><body bgcolor="#e6e6e6">
   
<center><b><font color = "black" size="6">Online Voting System</font></b></center><br><br>
<div id="page">
<div id="header">
<h2>Student Registration </h2>
<div class="news"><marquee behavior="alternate">Polling is going on!Login and go to Current Polls to vote for your favourate candidates. </marquee></div>
</div>
<div id="container">
<?php
require('connection.php');
//Process
if (isset($_POST['submit']))
{

$stdRN = addslashes( $_POST['rollno'] ); //prevents types of SQL injection
$stdName = addslashes( $_POST['sname'] ); //prevents types of SQL injection
$stdClass = $_POST['sclass'];//prevents types of SQL injection
$stdSex = $_POST['gender'];//prevents types of SQL injection
$myPassword = $_POST['password'];    
$stdPass = md5($myPassword); //This will make your password encrypted into md5, a high security hash  
    
$sql = mysqli_query($con, "INSERT INTO student (sl_no,roll_no,name,class,sex,password)
VALUES ('',$stdRN',$stdName','$stdClass', '$stdSex', '$stdPass') ") or die ('Error entering database');

die( "You have registered for an account.<br><br>Go to <a href=\"index.php\">Login</a>" );
}

echo "<center><h3>Register an account by filling in the needed information below:</h3></center><br><br>";
echo '<form action="registeracc.php" method="post" onsubmit="return registerValidate(this)">';
echo '<table align="center">';
echo "<tr><td>Roll Number:</td><td><input type='text' style='background-color:#e8daef; font-weight:regular;' name='rollno' maxlength='30' value=''></td></tr>";
echo "<tr><td>Name:</td><td><input type='text' style='background-color:#e8daef; font-weight:regular;' name='sname' maxlength='30' value=''></td></tr>";
echo "<tr><td>Class:</td><td><select name='sclass' style='background-color:#e8daef; font-weight:regular;' maxlength='10' id='class' required='true'>
    <option value='hs1'>HS-1st Year</option>
    <option value='hs2'>HS-2nd Year</option>
    <option value='ba1'>BA-1st Sem</option>
    <option value='ba3'>BA-3rd Sem</option>
    <option value='ba5'>BA-5th Sem</option>
    <option value='bcom1'>BCom-1st Sem</option>
    <option value='bcom3'>BCom-3rd Sem</option>
    <option value='bcom5'>BCom-5th Sem</option>        
  </select> 
  </td></tr>" ; 
echo "<tr><td>Sex:</td><td><input type='radio' style='background-color:#e8daef; font-weight:regular;' name='gender' id='male'  value='Male' checked>Male<br>   
<input type='radio' style='background-color:#e8daef; font-weight:regular;' name='gender' id='female'  value='Female'>Female<br></td></tr>"; 
echo "<tr><td>Password:</td><td><input type='password' style='background-color:#e8daef; font-weight:regular;' name='password' maxlength='10' value=''></td></tr>";
echo "<tr><td>Confirm Password:</td><td><input type='password' style='background-color:#e8daef; font-weight:regular;' name='ConfirmPassword' maxlength='20' value=''></td></tr>";
echo "<tr><td>&nbsp;</td><td><input type='submit' name='submit' value='Register Account'/></td></tr>";
echo "<tr><td colspan = '2'><p>Already have an account? <a href='index.php'><b>Login Here</b></a></td></tr>";
echo "</tr></td></table>";
echo "</form>";
?>
</div> 
<div id="footer">
<div class="bottom_addr">Student Union Election,My New College</div>
</div>
</div>
</body>
<script src="js/jquery-1.2.6.min.js"></script>
    <script>
    $(document).ready(function(){
      
        $('#roll').blur(function(event){
         
            event.preventDefault();
            var rollNo=$('#roll').val();
                                $.ajax({                     
                            url:'checkuser.php',
                            method:'post',
                            data:{roll:rollNo},  
                            dataType:'php',
                            success:function(message)
                            {
                            $('#result').php(message);
                            }
                      });  
        });

    });
   
    </script>
</html>

Code: Select all

[b]checkuser.php[/b]

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
extract($_REQUEST);

require_once('connection.php');
$q="SELECT * FROM student WHERE roll_no='$stdRN'";
$result=mysqli_query($con,$q);
if(mysqli_num_rows($result)){
    echo"Roll Number already Registerd Please Try Another";
}else{
    echo"";
}
?>

Code: Select all

[b]connection.php
[/b]
<?php
error_reporting(1);
$con=mysqli_connect('localhost', 'root', 'abc@123$%~','mypoll') or die(mysqli_error());
?>

Image
Attachments
database.png
database.png (87.93 KiB) Viewed 1097 times
User avatar
Michalio
Moderator
Moderator
Posts: 214
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

The form send the data to registeracc.php, probably it is a old register script that not works. If you are process the form on the same url then you can remove the action parameter from the form.
Additionaly do not pass any parametrrs directly to the query (read about sql injection). Use parameter binding to keep your app safe.
You can also avoid to echo the html block, the good practice is keep php and html separately. You can use the ?> abd <?php to end and start again php code
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
Post Reply