Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Database Inserting

Joined
May 17, 2007
Messages
2,474
Reaction score
681
Hi,

I'm currently trying to make a addon for HoloCMS ( habbo section )

Im trying to make a bot creator, and i try inserting the info in the text input to database, but it wont

heres the code:

PHP:
<?php 
/*===================================================+ 
|| # Addon By Zam and AJ
|+===================================================*/ 

require_once('../core.php'); 
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; } 
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; } 

$pagename = "addbot"; 

if(isset($_POST['id'])){

    if(!empty($_POST['name']) && !empty($_POST['id'])){ 

    $name = addslashes($_POST['name']);
    $roomid = addslashes($_POST['roomid']);
    $mission = addslashes($_POST['mission']);
    $figure = addslashes($_POST['figure']);
    $x = addslashes($_POST['x']);
    $y = addslashes($_POST['y']);
    $z = addslashes($_POST['z']);
    $freeroam = addslashes($_POST['freeroam']);
    $message_noshouting = addslashes($_POST['message_noshouting']);

             
            mysql_query("INSERT INTO roombots (id,name,freeroam) VALUES ('".addslashes($_POST['name'])."','".addslashes($_POST['roomid'])."','".addslashes($_POST['mission'])."','".addslashes($_POST['figure'])."','".addslashes($_POST['x'])."','".addslashes($_POST['y'])."','".addslashes($_POST['z'])."','".$_POST['freeroam']."','".$_POST['message_noshouting']."')") or die(mysql_error()); 
            $msg = "RoomBoT has been sucessfully made."; 

            mysql_query("INSERT INTO system_stafflog (action,message,note,userid,targetid,timestamp) VALUES ('Housekeeping','Created RoomBot ".addslashes($_POST['id'])." BotID ".addslashes($_POST['credits'])." credits','vouchers.php','".$my_id."','','".$date_full."')") or die(mysql_error()); 

        } 
    } else { 

        $msg = "Please Try To Fill In All Spaces."; 

    } 


@include('subheader.php'); 
@include('header.php'); 
?> 
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'> 
<tr> <td width='22%' valign='top' id='leftblock'> 
 <div> 
 <!-- LEFT CONTEXT SENSITIVE MENU --> 
<?php @include('usermenu.php'); ?> 
 <!-- / LEFT CONTEXT SENSITIVE MENU --> 
 </div> 
 </td> 
 <td width='78%' valign='top' id='rightblock'> 
 <div><!-- RIGHT CONTENT BLOCK --> 
  
<?php if(isset($msg)){ ?><p><strong><?php echo $msg; ?></p></strong><?php } ?> 
  
<form action='index.php?p=addbot&do=create' method='post' name='theAdminForm' id='theAdminForm'> 
<div class='tableborder'> 
<div class='tableheaderalt'>Create Bot</div> 

<?php
function randomid($code) {
$characters = "1234567890";
$key = $characters{rand(0,71)};
for($i=1;$i<$code;$i++)
    {
        $key .= $characters{rand(0,71)};
    }
    return $key;
}

?>

<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'> 
<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Bot Name</strong><div class='graytext'>Name of the bot.</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='name' value="" size='30' class='textinput'></td> 
</tr> 

<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Room ID</strong><div class='graytext'>The room this bot will be placed in.</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name=roomid' value="" size='30' class='textinput'></td> 
</tr> 

<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Mission</strong><div class='graytext'>The RoomBot's mission</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='mission' value="" size='30' class='textinput'></td> 
</tr> 

<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Create Bot' class='realbutton' accesskey='s'></td></tr> 
</form></table></div><br /> 

<?php 
$get_bots = mysql_query("SELECT id,name,freeroam FROM roombots") or die(mysql_error()); 
?> 

 <div class='tableborder'> 
 <div class='tableheaderalt'>Existing Bots</div> 
 <table cellpadding='4' cellspacing='0' width='100%'> 
 <tr> 
  <td class='tablesubheader' width='2%' align='center'>ID</td> 
  <td class='tablesubheader' width='40%' align='center'>Name</td> 
  <td class='tablesubheader' width='40%' align='center'>FreeRoam</td>
  <td class='tablesubheader' width='1%' align='center'>Edit</td>  

 </tr> 
<?php 

while($row = mysql_fetch_assoc($get_bots)){ 
    printf(" <tr> 
  <td class='tablerow1' align='center'>%s</td> 
  <td class='tablerow2' align='center'>%s</td> 
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'><a href='index.php?p=editbot&key=%s'><img src='./images/edit.gif' alt='Edit User Data'></a></td>
</tr>", $row['id'], $row['name'], $row['freeroam'], $row['edit']); 
} 
?> 
  
 </table> 
</div> 
     </div><!-- / RIGHT CONTENT BLOCK --> 
     </td></tr> 
</table> 
</div><!-- / OUTERDIV --> 
<div align='center'><br /> 
<?php 
$mtime = explode(' ', microtime()); 
$totaltime = $mtime[0] + $mtime[1] - $starttime; 
printf('Time: %.3f', $totaltime); 
?> 
</div>

Im still learning php
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
The page's framework looks nice, and the idea is nice, but the backend database transfer PHP is pretty sad.. I'll take a look at it, do some cleaning, that might help you. I might need more information to really make it work..
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
this code is more messed up then I thought.. Most of the work is indeed NOT there. I'm going to have to assume that '../core.php' is your substitute for "./connect.php"
If you don't have a connection include, the connection will never happen.

Secondly, I did make a good improvement to your code. I made it legible. I also went ahead and put a mysql_close(); statement so that your code didn't spark lag build-up. But, if there's no connection, I guess you don't need a close statement.. LoL!

Another thing, while simpling down the following code, I found an error.
PHP:
//insertion code:
             $insert1="INSERT INTO `roombots` (`id`,`name`,`freeroam`) VALUES ('$name','$roomid','$mission','$figure','$x','$y','$z','$freeroam','$message_noshouting')";
 
    //Query: 1
            mysql_query($insert1) or die("Could not connect to roombots"); 
            $msg = "RoomBoT has been sucessfully made.";
The error being this:
You're trying to insert name, roomid, mission, figure, x, y, z, freeroam, and message_noshouting into id, name, freeroam.
You can't just send this data to the sql abyse, it needs a place to go..
Make more fields in the sql database for it to work. Your code won't work unless the database can hold the values with fields.

After you do your sql work, your code should look something like this:
PHP:
//insertion code:
             $insert1="INSERT INTO `roombots` (`name`,`roomid`,`mission`,`figure`,`x`,`y`,`z`,`freeroam`,`message_noshouting`) VALUES ('$name','$roomid','$mission','$figure','$x','$y','$z','$freeroam','$message_noshouting')";
 
    //Query: 1
            mysql_query($insert1) or die("Could not connect to roombots"); 
            $msg = "RoomBoT has been sucessfully made.";

PHP:
<?php 
/*===================================================+ 
|| # Addon By Zam and AJ
|+===================================================*/ 
require_once('../core.php'); 
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; } 
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; } 
$pagename = "addbot"; 
if(isset($_POST['id'])){
    if(strlen($_POST['name'])>0 && strlen($_POST['id'])>0){ 
//Turn data floating in the "abyse" into varriables
    $name = $_POST['name'];
    $roomid = $_POST['roomid'];
    $mission = $_POST['mission'];
    $figure = $_POST['figure'];
    $x = $_POST['x'];
    $y = $_POST['y'];
    $z = $_POST['z'];
    $freeroam = $_POST['freeroam'];
    $message_noshouting = $_POST['message_noshouting'];
//insertion code:
             $insert1="INSERT INTO `roombots` (`id`,`name`,`freeroam`) VALUES ('$name','$roomid','$mission','$figure','$x','$y','$z','$freeroam','$message_noshouting')";
 
    //Query: 1
            mysql_query($insert1) or die("Could not connect to roombots"); 
            $msg = "RoomBoT has been sucessfully made."; 
//Turning more data floating in the "abyse" into varriables..
$id2=$_POST['id'];
$credits2=$_POST['credits'];
 
   $insert2="INSERT INTO `system_stafflog` (`action`,`message`,`note`,`userid`,`targetid`,`timestamp`) VALUES ('Housekeeping','Created RoomBot $id2 BotID $credits2 credits','vouchers.php','$my_id','','$date_full')";
            mysql_query($insert2) or die("Could not connect to system_stafflog"); 
 
//CLOSE YOUR DATABASE CONNECTION!!! Always. - s-p-n ;)
mysql_close();
        } 
    } else { 
        $msg = "Please Try To Fill In All Spaces."; 
    } 
 
@include('subheader.php'); 
@include('header.php'); 
?> 
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'> 
<tr> <td width='22%' valign='top' id='leftblock'> 
 <div> 
 <!-- LEFT CONTEXT SENSITIVE MENU --> 
<?php @include('usermenu.php'); ?> 
 <!-- / LEFT CONTEXT SENSITIVE MENU --> 
 </div> 
 </td> 
 <td width='78%' valign='top' id='rightblock'> 
 <div><!-- RIGHT CONTENT BLOCK --> 
 
<?php if(isset($msg)){ ?><p><strong><?php echo $msg; ?></p></strong><?php } ?> 
 
<form action='index.php?p=addbot&do=create' method='post' name='theAdminForm' id='theAdminForm'> 
<div class='tableborder'> 
<div class='tableheaderalt'>Create Bot</div> 
<?php
function randomid($code) {
$characters = "1234567890";
$key = $characters{rand(0,71)};
for($i=1;$i<$code;$i++)
    {
        $key .= $characters{rand(0,71)};
    }
    return $key;
}
?>
<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'> 
<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Bot Name</strong><div class='graytext'>Name of the bot.</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='name' value="" size='30' class='textinput'></td> 
</tr> 
<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Room ID</strong><div class='graytext'>The room this bot will be placed in.</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name=roomid' value="" size='30' class='textinput'></td> 
</tr> 
<tr> 
<td class='tablerow1'  width='40%'  valign='middle'><strong>Mission</strong><div class='graytext'>The RoomBot's mission</div></td> 
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='mission' value="" size='30' class='textinput'></td> 
</tr> 
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Create Bot' class='realbutton' accesskey='s'></td></tr> 
</form></table></div><br /> 
<?php 
$get_bots = mysql_query("SELECT id,name,freeroam FROM roombots") or die(mysql_error()); 
?> 
 <div class='tableborder'> 
 <div class='tableheaderalt'>Existing Bots</div> 
 <table cellpadding='4' cellspacing='0' width='100%'> 
 <tr> 
  <td class='tablesubheader' width='2%' align='center'>ID</td> 
  <td class='tablesubheader' width='40%' align='center'>Name</td> 
  <td class='tablesubheader' width='40%' align='center'>FreeRoam</td>
  <td class='tablesubheader' width='1%' align='center'>Edit</td>  
 </tr> 
<?php 
while($row = mysql_fetch_assoc($get_bots)){ 
    printf(" <tr> 
  <td class='tablerow1' align='center'>%s</td> 
  <td class='tablerow2' align='center'>%s</td> 
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'><a href='index.php?p=editbot&key=%s'><img src='./images/edit.gif' alt='Edit User Data'></a></td>
</tr>", $row['id'], $row['name'], $row['freeroam'], $row['edit']); 
} 
?> 
 
 </table> 
</div> 
     </div><!-- / RIGHT CONTENT BLOCK --> 
     </td></tr> 
</table> 
</div><!-- / OUTERDIV --> 
<div align='center'><br /> 
<?php 
$mtime = explode(' ', microtime()); 
$totaltime = $mtime[0] + $mtime[1] - $starttime; 
printf('Time: %.3f', $totaltime); 
?> 
</div>

I only change the very top processing part. I didn't look into the rest of the page. Hope this helps!
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I think you need a connecter..

do you have a page called connect.php?

If you do, put this at or around the top of your page:
PHP:
<?php
@include("./connect.php");
?>
 
Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
If you want to make multiple querys without keep on
PHP:
//1
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

//2
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

//3
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

I dont even think i use mysql_close anywhere tbfh xD
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
PHP:
//1
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

//2
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

//3
mysql_connect();
mysql_select_db();
mysql_query();
mysql_close();

Best to only use connect and close once per page per host ;)
 
Back
Top