Jump to content

Database problem probably caused by a php error


DEVILofDARKNESS

Recommended Posts

If I press on the link become ally on the page empire.php?nation_id=1

I'm redirected to relation.php?type=Ally&between=usus&send=2&receive=1,

That page insert that values in the database,

and redirect to the empire.php?nation_id=1 page,

EVerything works fine, except their isn't any value insert in my database :-s

 

empire.php (I've cut out almost whole the code, because it is only a litle piece that is used for this, if their is missing something tell me and I will give it :) )

<?php
session_start();
require_once "login-check.php";
/*DATABASE SETTINGS */
$username = $_SESSION['username'];
$query = "SELECT user_id FROM users WHERE user_name = '$username'";
$result = mysql_query($query);
list($ownid) = mysql_fetch_row($result);
$nationid = (int)$_GET["nation_id"];
$query = "SELECT nation_ruler_id FROM nations WHERE nation_id = '$nationid'";
$result = mysql_query($query);
list($id) = mysql_fetch_row($result);
$query = "SELECT type FROM diplomacy WHERE ((send = '$id' && received = '$ownid') || (send = '$ownid' && received = '$id'))";
$result = mysql_query($query);
list($relation) = mysql_fetch_row($result);
if(isset($relation)) {
if($relation == Ally) {
	$relationtype = "You're Allies!";
}elseif($relation == Enemy) {
	$relationtype = "OoO, You're Enemies!";
}elseif($relation == Secret . " " . Pact) {
	$relationtype = "You have a Secret Pact! Shht!";
}elseif($relation == NAP) {
	$relationtype = "You have a Non Attack Pact(NAP)";
}
}else{
$relationtype = 'Allies: <a href="relation.php?type=Ally&between=usus&send=' . $ownid . '&receive=' . $id . '">Become Ally!</a><br>
							Enemies: <a href="relation.php?type=Enemy&between=usus&send=' . $ownid . '&receive=' . $id . '">Become Enemy!</a><br>
							NAP: <a href="relation.php?type=NAP&between=usus&send=' . $ownid . '&receive=' . $id . '">Declare a NAP!</a> (Non Attacking pact)<br>
							SP: <a href="relation.php?type=Secret' . ' ' . 'Pact&between=usus&send=' . $ownid . '&receive=' . $id . '">Declare a SP!</a> (Secret Pact)';
}
?>
<html>
<body>
<?php
$allianceid = $nation['alliance_id'];
$query = "SELECT * FROM pacts WHERE alliance_id = '$allianceid'";
$result = mysql_query($query);
while($pact = mysql_fetch_array($result)) {
?>
				<a href="alliance.php?alliance_id=<?php echo $pact['alliance_id']; ?>&type=info"><img src="<?php echo $pact['alliance_image']; ?>" border="0" align="left" height="125px" width="125px"></img></a>
							Alliance Name: <?php echo $pact['alliance_name']; ?> <br>
							<?php echo $relationtype; ?>
</body>

 

relation.php (full code)

<?php
require_once 'config.php';
	 $conn = mysql_connect($dbhost,$dbuser,$dbpass)
                or die ('Error connecting to mysql');
        mysql_select_db($dbname);
$type = $_GET['type'];
$between = $_GET['between'];
$send = $_GET['send'];
$received = $_GET['receive'];
$query = "INSERT INTO diplomacy(send,received,type,between) VALUES ('$send','$received','$type','$between')";
$result = mysql_query($query);
header('location: /empire.php?nation_id=' . $received);
?>

Link to comment
Share on other sites

The description of your problem is near impossible to understand but I will say this.

 

On numerous occasions you execute queries then attempt to use there results without first checking your queries succeed. This is one of the basics of programming. Check results are what you expect before attempting to use them. eg; A typical SELECT query should like like....

 

if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // in here $result is guaranteed to hold valid results.
  } else {
    // no records matched your query.
    // handle error
  }
} else {
  // your query failed.
  // handle error.
}

Link to comment
Share on other sites

So it isn't clear...

I will try to explain it again :)

 

Their is a link on a certain page.(empire.php)

if U click on that link U goto relation.php,

where the query should be executed.

if it is executed you are auto redirected to the first page.

 

Should it be?

$query = "INSERT INTO diplomacy(send,received,type,between) VALUES ('$send','$received','$type','$between')";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
    // What should happen here?
  } else {
    echo "Their was no match";
  }
} else {
  echo "their was an error in your sql syntax."
}

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.