数据库无法插入值

时间:2016-10-13 16:26:13

标签: php html

当我插入值时,它表示检查数据库连接。所有的事情都是正确的,但它总是给出相同的错误。 我正在参加测验,此页面用于添加问题。每当我插入它所说的值时,请检查数据库连接。所有事情都是正确的。用户名密码数据库名称也是正确的。 但是同样的错误正在发生

<!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>

1 个答案:

答案 0 :(得分:3)

你的&#34;检查数据库连接&#34;错误消息是无用的。永远不要输出固定的不变信息。让数据库 TELL 您查询失败的原因:

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

请注意,您只需确定即可正常运行。您从不打扰检查连接和select_db调用的返回值。