-1

when ever i am insert the value it says check data base connection. All the things are correct but it always giving the same error. I am amking the quiz this page is for adding the question adding. whenever i am inserting the value it says please check DB connection. All the things are correct. User name password database name are also correct. But same error is occuring

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>!deal Banker</title>
<style type="text/css">

body{
margin:0;
padding:0;
line-height: 1.5em;
}

b{font-size: 110%;}
em{color: red;}

#topsection{
background: #EAEAEA;
height: 90px; /*Height of top section*/
}

#topsection h1{
margin: 0;
padding-top: 15px;
}

#contentwrapper{
float: left;
width: 100%;
}

#contentcolumn{
margin: 0 200px 0 230px; /*Margins for content column. Should be "0 RightColumnWidth 0 LeftColumnWidth*/
}

#leftcolumn{
float: left;
width: 230px; /*Width of left column*/
margin-left: -100%;
background: #C8FC98;
}

#rightcolumn{
float: left;
width: 200px; /*Width of right column*/
margin-left: -200px; /*Set left marginto -(RightColumnWidth)*/
background: #FDE95E;
}

#footer{
clear: left;
width: 100%;
background: black;
color: #FFF;
text-align: center;
padding: 4px 0;
}

#footer a{
color: #FFFF80;
}

.innertube{
margin: 10px; /*Margins for inner DIV inside each column (to provide padding)*/
margin-top: 0;
}

/* ####### responsive layout CSS ####### */

@media (max-width: 840px){ /* 1st level responsive layout break point- drop right column down*/

    #leftcolumn{
    margin-left: -100%;
    }

    #rightcolumn{
    float: none;
    width: 100%;
    margin-left: 0;
    clear: both;
    }

    #contentcolumn{
    margin-right: 0; /*Set margin to LeftColumnWidth*/
    }
}

@media (max-width: 600px){ /* 2nd level responsive layout break point- drop left column down */
    #leftcolumn{
    float: none;
    width: 100%;
    clear: both;
    margin-left: 0;
    }

    #contentcolumn{
    margin-left: 0;
    }
}

</style>



</head>
<body>
<div id="maincontainer">

<div id="topsection"><div class="innertube">
<?php
include'menu.php';

?></div></div>

<div id="contentwrapper">
<div id="contentcolumn">
<?php

$conn = mysql_connect("localhost","banker","gaurav@441");
$db = "idealbanker";
$table = "pp";
mysql_select_db($db,$conn);

if(isset($_REQUEST['submit']))
{
if($_POST['question'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer1'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer2'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer3'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer4'] == '')
{
echo 'cannot submit field empty';
}
elseif($_POST['answer5'] == '')
{
echo 'cannot submit field empty';
}
else
{
function clean($str) {
                $str = @trim($str);
                if(get_magic_quotes_gpc()) {
                        $str = stripslashes($str);
                }
                return mysql_real_escape_string($str);
        }

$question = clean($_POST['question']);
$answer1 = clean($_POST['answer1']);
$answer2 = clean($_POST['answer2']);
$answer3 = clean($_POST['answer3']);
$answer4 = clean($_POST['answer4']);
$correctanswer = clean($_POST['answer5']);
$qry = "INSERT INTO $table( question, answer1, answer2, answer3, answer4, correctanswer )VALUES('$question','$answer1','$answer2','$answer3','$answer4','$correctanswer')";
        $result = @mysql_query($qry);
if(!$result)
{
echo 'Question Cannot Submit Check DB Connection';
echo "<br><a href=".$_SERVER['PHP_SELF'].">Add Again</a>";
}
else
{
echo 'Question Submitted Successfully';
}
echo "<br><a href=".$_SERVER['PHP_SELF'].">Add More</a>";
}
}
else
{
echo '<form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'">
  <table width="500" border="0">
    <tr>
      <td width="100">Question</td>
      <td width="242">
        <input name="question" type="text" size="60">
</td>
    </tr>
    <tr>
      <td>Answer 1 </td>
      <td><input name="answer1" type="text" size="30"></td>
    </tr>
    <tr>
      <td>Answer 2 </td>
      <td><input name="answer2" type="text" size="30"></td>
    </tr>
    <tr>
      <td>Answer 3 </td>
      <td><input name="answer3" type="text" size="30"></td>
    </tr>
    <tr>
      <td>Answer 4 </td>
      <td><input name="answer4" type="text" size="30"></td>
    </tr>
    <tr>
      <td>Correct Answer </td>
      <td><input name="answer5" type="text" size="30"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="submit" value="Submit">
      </label></td>
    </tr>
  </table>
</form>
';
}
?>
</div>
</div>

<div id="leftcolumn">
<?php
include 'left.php';
?>

</div>

<div id="rightcolumn">
<?php
include 'right.php';
?>
</div>

<div id="footer"><?php
include'footer.php';

?></div>

</div>

</body>
</html>
  • Any reason why you are using deprecated mysql_connect? http://php.net/manual/en/function.mysql-connect.php – Liam C Oct 13 '16 at 16:28
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 13 '16 at 16:32

1 Answers1

3

Your "check db connection" error message is UTTERLY useless. Never output a fixed unchanging message. Have the DB TELL you why the query failed:

if (!$result) {
   die(mysql_error());
}

And note that you're simply ASSUMING the connection is working. You never bother checking the return values of your connection and select_db calls.

Marc B
  • 356,200
  • 43
  • 426
  • 500