Jump to content

Function Problem?


master82

Recommended Posts

Below is some code I have on a page, I fill in a form and submit and it should all this function - but its not actually populating the database table, can you see anything wrong in this?

[code]
<?php

//code

{
case "addcity": addcity(); break;
case "editcity": editcity(); break;
case "delcity": delcity(); break;
default: print "Error: This script requires an action."; break;
}
function addcity()
{
global $db, $ir, $c, $h, $userid;
$minlevel=abs((int) $_POST['minlevel']);
$name=$_POST['name'];
$desc=$_POST['desc'];
if($minlevel and $desc and $name)
{
$q=$db->query("SELECT * FROM cities WHERE cityname='{$name}'");
if($db->num_rows($q))
{
print "Sorry, you cannot have two cities with the same name.";
$h->endpage();
exit;
}
$db->query("INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')");
print "City {$name} added to the game.";
stafflog_add("Created City $name");
}
else
{

//code

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29346-function-problem/
Share on other sites

No errors - just nothing in the database

[code]
<?php
include "sglobals.php";
if($ir['user_level'] > 2)
{
die("403");
}
//This contains city stuffs
switch($_GET['action'].)
{
case "addcity": addcity(); break;
case "editcity": editcity(); break;
case "delcity": delcity(); break;
default: print "Error: This script requires an action."; break;
}
function addcity()
{
global $db, $ir, $c, $h, $userid;
$minlevel=abs((int) $_POST['minlevel']);
$name=$_POST['name'];
$desc=$_POST['desc'];
if($minlevel and $desc and $name)
{
$q=$db->query("SELECT * FROM cities WHERE cityname='{$name}'");
if($db->num_rows($q))
{
print "Sorry, you cannot have two cities with the same name.";
$h->endpage();
exit;
}
$db->query("INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')");
print "City {$name} added to the game.";
stafflog_add("Created City $name");
}
else
{
print "<h3>Add City</h3><hr />
<form action='staff_cities.php?action=addcity' method='post'>
Name: <input type='text' name='name' /><br />
Description: <input type='text' name='desc' /><br />
Minimum Level: <input type='text' name=minlevel' /><br />

<input type='submit' value='Add City' /></form>";
}
}
function editcity()
{
global $db, $ir, $c, $h, $userid;
switch($_POST['step'])
{
case "2":
$minlevel=abs((int) $_POST['minlevel']);
$name=$_POST['name'];
$desc=$_POST['desc'];
$q=$db->query("SELECT * FROM cities WHERE cityname='{$name}' AND cityid!={$_POST['id']}");
if($db->num_rows($q))
{
print "Sorry, you cannot have two cities with the same name.";
$h->endpage();
exit;
}
$name=$_POST['name'];
$q=$db->query("SELECT * FROM cities WHERE cityid={$_POST['id']}");
$old=$db->fetch_row($q);
$db->query("UPDATE cities SET cityminlevel=$minlevel, citydesc='$desc', cityname='$name' WHERE cityid={$_POST['id']}");
print "City $name was edited successfully.";
stafflog_add("Edited city $name");
break;
case "1":
$q=$db->query("SELECT * FROM cities WHERE cityid={$_POST['city']}");
$old=$db->fetch_row($q);
print "<h3>Editing a City</h3><hr />
<form action='staff_cities.php?action=editcity' method='post'>
<input type='hidden' name='step' value='2' />
<input type='hidden' name='id' value='{$_POST['city']}' />
Name: <input type='text' name='name' value='{$old['cityname']}' /><br />
Description: <input type='text' name='desc' value='{$old['citydesc']}' /><br />
Minimum Level: <input type='text' name='minlevel' value='{$old['cityminlevel']}' /><br />
<input type='submit' value='Edit City' /></form>";
break;
default:
print "<h3>Editing a City</h3><hr />
<form action='staff_cities.php?action=editcity' method='post'>
<input type='hidden' name='step' value='1' />
City: ".location_dropdown($c, "city")."<br />
<input type='submit' value='Edit City' /></form>";
break;
}
}
function delcity()
{
global $db,$ir,$c,$h,$userid;
if($_POST['city'])
{
$q=$db->query("SELECT * FROM cities WHERE cityid={$_POST['city']}");
$old=$db->fetch_row($q);
if($old['cityid']==1)
{
die("This city cannot be deleted.");
}

$db->query("UPDATE users SET location=1 WHERE location={$old['cityid']}");
$db->query("UPDATE shops SET shopLOCATION=1 WHERE shopLOCATION={$old['cityid']}");
$db->query("DELETE FROM cities WHERE cityid={$old['cityid']}");
print "City {$old['cityname']} deleted.";
stafflog_add("Deleted city {$old['cityname']}");
}
else
{
print "<h3>Delete City</h3><hr />
Deleting a city is permanent - be sure. Any users and shops that are currently in the city you delete will be moved to the default city (ID 1).<form action='staff_cities.php?action=delcity' method='post'>
City: ".location_dropdown($c, "city")."<br />
<input type='submit' value='Delete City' /></form>";
}
}
function report_clear()
{
global $db,$ir,$c,$h,$userid;
if($ir['user_level'] > 3)
{
die("403");
}
$_GET['ID'] = abs((int) $_GET['ID']);
stafflog_add("Cleared player report ID {$_GET['ID']}");
$db->query("DELETE FROM preports WHERE prID={$_GET['ID']}");
print "Report cleared and deleted!<br />
<a href='staff_users.php?action=reportsview'>&gt; Back</a>";
}
$h->endpage();
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29346-function-problem/#findComment-134578
Share on other sites

i'm assuming that when you posted most of your 'addcity' function to start with, youre talking about that one in particular. we'll start there anyway:
can you add an extra line for me, just to make sure the data is being populated into the query:
where you have:
[code]
$db->query("INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')");
[/code]
in addcity, can you change it to:
[code]
$query = "INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')";
echo $query;
$db->query($query);
[/code]
and tell me what the query is when you add a new city?
Link to comment
https://forums.phpfreaks.com/topic/29346-function-problem/#findComment-134580
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.