I am trying to use mySQL query with data from POST. I am a total beginner. The issue is the script do not print me any results of the query.
Here is the form:
<form method="post" action="add.php">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Data dd/mm/rr</td>
<td><input name="data" type="date" id="data"></td>
</tr>
<tr>
<td width="100">First</td>
<td><input name="first" type="text" id="first" maxlength="2"></td>
</tr>
<tr>
<td width="100">Second</td>
<td><input name="second" type="text" id="second" maxlength="2"></td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add">
</td>
</tr>
</table>
</form>
And here is add.php
<html>
<body>
<link rel="stylesheet" href="mystyle.css">
<meta charset="utf-8">
<head>
<title>Lottery</title>
</head>
<?php
if(isset($_POST['submit'])){
$value = $_POST['Value'];
echo "Chosen value : " .$value;
}
?>
<div class="results">
<p>Counter: <? echo "".$value ?></p>
<p>Place: <? echo "".$value ?>: </p>
<p>Best match<? echo "".$value ?> :</p>
</div>
<?
$servername = "localhost";
$username = "lottery_root";
$password = "xyz";
$database = "lottery";
$conn = mysqli_connect($servername, $username, $password, $database) or die(mysqli_error($conn));
$result = mysqli_query($conn, "Select count(*) from lottery where first='$_SESSION[value]' or second='$_SESSION[value]'; ");
if (!$result) echo mysqli_error($conn);
$row = mysqli_fetch_row($result);
print_r($row);
?>
</body>
</html>
@MMK It looks like this and works better this is what it prints. Array ( [0] => 0 ) But 0 is not the correct value. Doesnt matter how many records match this query it always return 0. There is connection to db and queries are executing because when I use for example SELECT * FROM lottery; it prints whole record, but just one, not all of it.
You have mixed up mysql_*
functions with mysqli_*
. Next thing: You missed fourth parameter in MySQL connection (when creating new Mysqli object). Aaand, don't use PHP short tags, they are deprecated. Instead of using <?
use <?php
.
Second thing, how do You set value
key to session? Maybe You should use $_POST
global array in Your query.
Try this:
$servername = "localhost";
$username = "lottery_root";
$password = "xyz";
$database = "xxx";
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$result = $mysqli->query("SELECT COUNT(*) FROM lottery WHERE first='".$_POST['first']."' or druga='".$_POST['second']."' ");
if (!$result) {
echo $conn->error;
}
$row = $result->fetch_row();
print_r($row);
$conn->close();
it should be
<?php echo "".$value ?>
not
<? echo "".$value ?>
You have use mysqli for connections while you are not using $conn obj in firing query.
try this
$value = $_SESSION['value'];
if($result = $conn->query("select count(*) from lottery where first='$value' or druga='$value'")){
if(mysqli_num_row($result) > 0){
$row = mysqli_fetch_object($results);
print_r($row);
}
}else{
print_r($conn->error)
}
$conn->close();
I don't find any field with name="Value"
As in add.php you are expecting some value in $_POST['Value']
since there is no field with this name so $_POST['Value']
is not even set.
You're using mysqli
to connect and using mysql
to get data. Secondly why are you not selecting database
?
$servername = "localhost";
$username = "lottery_root";
$password = "xyz";
$db = "yourDB";
$conn = mysqli_connect($servername, $username, $password, $db) or die(mysqli_error($conn));
$result = mysqli_query($conn, "Select count(*) from lottery where first='$_SESSION[value]' or second='$_SESSION[value]'; ");
if (!$result) echo mysqli_error($conn);
$row = mysqli_fetch_row($result);
print_r($row);
?>