Jump to content

[SOLVED] How to check that there are no values already in a database field?


dessolator

Recommended Posts

Hi, I was wondering how I would check that there are no values already in the database? with php and mysql

 

Basically what I'm doing is a games system where people play 3 games, and when I create a fixture I want it to check that there are no values already in the database for those users (users are identified by a unique ID) Iif there are entries display error message if not execute insert into database statement.

 

Here is my code so far:

 

fixtureform.php

 

<?php  
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="abc123"; // Mysql password
$db_name="games_db1"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
<form id='form1' name='form1' method='post' action='gameprocessform.php'>
<center>
<b>Arrange a match</b><br/ ><br/ >
<select name='select'>
<?php
//FIRST DROP DOWN
echo "<center>";
//Database - Assigns the below statement into the variable query
$query = "SELECT id, forename, surname, class, house FROM members WHERE house = 'yellow' AND permissions ='3'";
$result = mysql_query($query);
//$row = mysql_fetch_row($result);
while($row = mysql_fetch_array($result))
  {
echo"<option style='background: yellow' value='$row[0]'>Name: $row[1] $row[2] | Class: $row[3] | House: $row[4]</option>";
}
echo "</center>";


$query1 = "SELECT id, forename, surname, class, house FROM members WHERE house = 'green' AND permissions ='3'";
$result1 = mysql_query($query1);
echo "<center>";
while($row = mysql_fetch_array($result1))
  {
echo"<option style='background: green' value='$row[0]'>Name: $row[1] $row[2] | Class: $row[3] | House: $row[4]</option>";
}
echo "</center>";
?>
</select>

<br/ >VS<br/ >


<select name='select1'>
<?php
//SECOND DROP DOWN
echo "<center>";
//Database - Assigns the below statement into the variable query
$query2 = "SELECT id, forename, surname, class, house FROM members WHERE house = 'yellow' AND permissions ='3'";
$result2 = mysql_query($query2);
//$row = mysql_fetch_row($result);
while($row = mysql_fetch_array($result2))
  {
echo"<option style='background: yellow' value='$row[0]'>Name: $row[1] $row[2] | Class: $row[3] | House: $row[4]</option>";
}
echo "</center>";


$query3 = "SELECT id, forename, surname, class, house FROM members WHERE house = 'green' AND permissions ='3'";
$result3 = mysql_query($query3);
echo "<center>";
while($row = mysql_fetch_array($result3))
  {
echo"<option style='background: green' value='$row[0]'>Name: $row[1] $row[2] | Class: $row[3] | House: $row[4]</option>";
}
echo "</center>";
?>
</select>
</center>  


<?php 
//GAMES DROP DOWN 
$query = "SELECT game FROM games";
$result = mysql_query($query);
?>
<center>
<br/ >GAME<br/ >
<select name='select2'>
<?php
while($row = mysql_fetch_array($result))
  {

echo"<option style='background: blue'  value='$row[0]'>$row[0]</option>";
}
?>
</select>
  
  <br/ ><br/>  
  <input type="submit" value="Submit" />
  </center>
</form>
</body>
</html>

 

 

 

fixtureprocessform.php

 

<?php
ob_start();
$host="localhost"; // Host name
$dbusername="root"; // Mysql username
$password="abc123"; // Mysql password
$db_name="games_db1"; // Database name
$tbl_name="members"; // Table name

$first_student= substr($_POST['select'], 0, 99);
$second_student= substr($_POST['select1'], 0, 99);
$game=substr($_POST['select2'], 0, 99);

echo $first_student;
echo "\n";
echo $second_student;
echo "\n";
echo $game;
echo "\n";


if ($first_student == $second_student){
echo "Sorry you must select two different students, one from the yellow house and one from the green house.";
exit (0);
}
else{
//Getting house and class for first user from db and storing it in variables
mysql_connect("$host", "$dbusername", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Database - Assigns the below statement into the variable query
$query = "SELECT house, class FROM members WHERE id = '$first_student'";
$result = mysql_query($query);

if($row = mysql_fetch_array($result))
  {
   $firstuser_house = $row[0];
   $firstuser_class = $row[1];
}
else{
echo "No users returned";
}
echo $firstuser_house;
echo $firstuser_class;


//Getting house and class for SECOND user from db and storing it in variables
mysql_connect("$host", "$dbusername", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

//Database - Assigns the below statement into the variable query
$query = "SELECT house, class FROM members WHERE id = '$second_student'";
$result = mysql_query($query);

/*If there is a result (which there will only be 1 as the UID is a unique ID) 
assign the house and class values for the student in 2 separate variables*/
if($row = mysql_fetch_array($result))
  {
   $seconduser_house = $row[0];
   $seconduser_class = $row[1];
}
//If no user is found for that ID ecbo error message
else{
echo "No users returned";
}
echo $seconduser_house;
echo $seconduser_class;
}
//Validation so that people from the same class or house can't play each other
if ($firstuser_house == $seconduser_house){
echo "You cannot play someone from the same house";
exit (0);
}

if ($firstuser_class == $seconduser_class){
echo "You cannot play someone from the same class";
exit (0);
}

?>


 

 

 

Thanks,

 

Ian

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.