Jump to content
Killersites Forums

PHP OOP Inserting records into db


jbwebdesign
 Share

Recommended Posts

hello, i'm new to Object Oriented Programming and I am trying to create a class function that will insert data into my database.

 

i beleive i have the code correct but i'm not sure what i am doing wrong. can some one help me please?

 

here is my code.....

 

class cms{

var $db;

var $host;

var $username;

var $password;

var $school;

var $country;

var $city;

var $state;

var $zip;

 

function connect(){

$con = mysql_connect($this->host,$this->username,$this->password) or die("Could not connect to database");

$sel =mysql_select_db($this->db) or die("Could not select database");

}

 

function newSchool(){

$sql = mysql_query("INSERT INTO schools (school, country, city, state, zip) VALUES (" . $this->school . ',' . $this->country . ',' . $this->city . ',' . $this->state . ',' . $this->zip . ")");

}

}

 

 

and here is my other code........

 

 

<?php

include("config.php");

$cms = new cms();

$cms->connect();

if($_GET['new_school'] == "yes"){

$cms->newSchool();

}

?>

<form action="new_school.php?new_school=yes" method="post">

<center><table align="center" border="1">

<tr>

<td>School Name: </td><td><input type="text" name="school_name" /></td>

</tr>

<tr>

<td>Country: </td><td><?=$cms->country(); ?></td>

</tr>

<tr>

<td>City: </td><td><input type="text" name="city" /></td>

</tr>

<tr>

<td>State: </td><td><input type="text" name="state" /></td>

</tr>

<tr>

<td>Zip: </td><td><input type="text" name="zip" /></td>

</tr>

<tr>

<td></td><td><input type="submit" name="submit" value="Add School!" /></td>

</tr>

</table>

 

and the config file just defines my $_POST vars for the form.

Link to comment
Share on other sites

this is my classlib.php

<?php 
//------THIS FILE WILL CONTROL MY CMS SYSTEM
class cms{
   //----------------DEFINE MY VARIABLES FOR THIS CMS CLASS    
       var $db;
       var $host;
       var $username;
       var $password;
       var $current_time;
       var $id;
       var $c_id;
       var $school;
       var $country;
       var $city;
       var $state;
       var $zip;
   //-----------------END DEFINE VARIABLES FOR THIS CLASS----
   function connect(){
       $con = mysql_connect($this->host,$this->username,$this->password) or die("Could not connect to database");
       $sel =mysql_select_db($this->db) or die("Could not select database");
   }
   function post(){
       $sql = "SELECT * FROM posts WHERE school_id = '".$this->id."' AND class_id = '" . $this->c_id . "' ORDER BY post_id DESC";
       $res = mysql_query($sql) or die(mysql_error());
   while($row = mysql_fetch_assoc($res)){
       echo '<h2 class="title"><a href="#">' . $row['post_title'] . '</a></h2>';
       echo '<p class="meta">' . $row['post_time'] . '
<span>Posted by <a href="#">' . $row['username'] . '</a></span></p>';
       echo '<div class="entry">
                   <p>'.$row['post_body'] . '</p>';
       echo '</div>';
       }
   }    
   function adminPost(){
       $sql = "SELECT * FROM admin_posts ORDER BY id DESC";
       $res = mysql_query($sql) or die(mysql_error());
   while($row = mysql_fetch_assoc($res)){
       echo '<h2 class="title"><a href="#">' . $row['post_title'] . '</a></h2>';
       echo '<p class="meta">' . $this->current_time . '
<span>Posted by <a href="#">' . $row['post_by'] . '</a></span></p>';
       echo '<div class="entry">
                   <p>' . $row['post_body'] . '</p>';
       echo '</div>';
       }
   }
   function newSchool(){
       $sql = mysql_query("INSERT INTO schools (school, country, city, state, zip) VALUES (" . $this->school . ',' . $this->country . ',' . $this->city . ',' . $this->state . ',' . $this->zip . ")");
   }
   function schoolList(){
       $sql = "SELECT * FROM schools";
       $res = mysql_query($sql) or die(mysql_error());
   while($row = mysql_fetch_assoc($res)){
       echo '<div class="entry">
                   <p><a href="classes.php?school_id=' . $row['id'] . '">' . $row['school'] . '</a></p>';
       echo '</div>';
       }
   }    
   function classList(){
       $sql = "SELECT * FROM classes WHERE school_id = '".$this->id."' ORDER BY class_type DESC";
       $res = mysql_query($sql) or die(mysql_error());
   while($row = mysql_fetch_assoc($res)){
       echo '<div class="entry">
                   <p><a href="class_posts.php?school_id=' . $row['id'] . '&class_id=' . $row['class_id'] . '">' . $row['class_type'] . '</a> - With ' . $row['teacher'] . '</p>';
       echo '</div>';
       }
   }    
   function country(){
       echo '<select name="country">
     <option value=" " selected>(please select a country)</option>
     <option value="--">none</option>
     <option value="AF">Afghanistan</option>
     <option value="AL">Albania</option>
     <option value="DZ">Algeria</option>
     <option value="AS">American Samoa</option>
     <option value="AD">Andorra</option>
     <option value="AO">Angola</option>
     <option value="AI">Anguilla</option>
     <option value="AQ">Antarctica</option>
     <option value="AG">Antigua and Barbuda</option>
     <option value="AR">Argentina</option>
     <option value="AM">Armenia</option>
     <option value="AW">Aruba</option>
     <option value="AU">Australia</option>
     <option value="AT">Austria</option>
     <option value="AZ">Azerbaijan</option>
     <option value="BS">Bahamas</option>
     <option value="BH">Bahrain</option>
     <option value="BD">Bangladesh</option>
     <option value="BB">Barbados</option>
     <option value="BY">Belarus</option>
     <option value="BE">Belgium</option>
     <option value="BZ">Belize</option>
     <option value="BJ">Benin</option>
     <option value="BM">Bermuda</option>
     <option value="BT">Bhutan</option>
     <option value="BO">Bolivia</option>
     <option value="BA">Bosnia and Herzegowina</option>
     <option value="BW">Botswana</option>
     <option value="BV">Bouvet Island</option>
     <option value="BR">Brazil</option>
     <option value="IO">British Indian Ocean Territory</option>
     <option value="BN">Brunei Darussalam</option>
     <option value="BG">Bulgaria</option>
     <option value="BF">Burkina Faso</option>
     <option value="BI">Burundi</option>
     <option value="KH">Cambodia</option>
     <option value="CM">Cameroon</option>
     <option value="CA">Canada</option>
     <option value="CV">Cape Verde</option>
     <option value="KY">Cayman Islands</option>
     <option value="CF">Central African Republic</option>
     <option value="TD">Chad</option>
     <option value="CL">Chile</option>
     <option value="CN">China</option>
     <option value="CX">Christmas Island</option>
     <option value="CC">Cocos (Keeling) Islands</option>
     <option value="CO">Colombia</option>
     <option value="KM">Comoros</option>
     <option value="CG">Congo</option>
     <option value="CD">Congo, the Democratic Republic of the</option>
     <option value="CK">Cook Islands</option>
     <option value="CR">Costa Rica</option>
     <option value="CI">Cote d'Ivoire</option>
     <option value="HR">Croatia (Hrvatska)</option>
     <option value="CU">Cuba</option>
     <option value="CY">Cyprus</option>
     <option value="CZ">Czech Republic</option>
     <option value="DK">Denmark</option>
     <option value="DJ">Djibouti</option>
     <option value="DM">Dominica</option>
     <option value="DO">Dominican Republic</option>
     <option value="TP">East Timor</option>
     <option value="EC">Ecuador</option>
     <option value="EG">Egypt</option>
     <option value="SV">El Salvador</option>
     <option value="GQ">Equatorial Guinea</option>
     <option value="ER">Eritrea</option>
     <option value="EE">Estonia</option>
     <option value="ET">Ethiopia</option>
     <option value="FK">Falkland Islands (Malvinas)</option>
     <option value="FO">Faroe Islands</option>
     <option value="FJ">Fiji</option>
     <option value="FI">Finland</option>
     <option value="FR">France</option>
     <option value="FX">France, Metropolitan</option>
     <option value="GF">French Guiana</option>
     <option value="PF">French Polynesia</option>
     <option value="TF">French Southern Territories</option>
     <option value="GA">Gabon</option>
     <option value="GM">Gambia</option>
     <option value="GE">Georgia</option>
     <option value="DE">Germany</option>
     <option value="GH">Ghana</option>
     <option value="GI">Gibraltar</option>
     <option value="GR">Greece</option>
     <option value="GL">Greenland</option>
     <option value="GD">Grenada</option>
     <option value="GP">Guadeloupe</option>
     <option value="GU">Guam</option>
     <option value="GT">Guatemala</option>
     <option value="GN">Guinea</option>
     <option value="GW">Guinea-Bissau</option>
     <option value="GY">Guyana</option>
     <option value="HT">Haiti</option>
     <option value="HM">Heard and Mc Donald Islands</option>
     <option value="VA">Holy See (Vatican City State)</option>
     <option value="HN">Honduras</option>
     <option value="HK">Hong Kong</option>
     <option value="HU">Hungary</option>
     <option value="IS">Iceland</option>
     <option value="IN">India</option>
     <option value="ID">Indonesia</option>
     <option value="IR">Iran (Islamic Republic of)</option>
     <option value="IQ">Iraq</option>
     <option value="IE">Ireland</option>
     <option value="IL">Israel</option>
     <option value="IT">Italy</option>
     <option value="JM">Jamaica</option>
     <option value="JP">Japan</option>
     <option value="JO">Jordan</option>
     <option value="KZ">Kazakhstan</option>
     <option value="KE">Kenya</option>
     <option value="KI">Kiribati</option>
     <option value="KP">Korea, Democratic People's Republic of</option>
     <option value="KR">Korea, Republic of</option>
     <option value="KW">Kuwait</option>
     <option value="KG">Kyrgyzstan</option>
     <option value="LA">Lao People's Democratic Republic</option>
     <option value="LV">Latvia</option>
     <option value="LB">Lebanon</option>
     <option value="LS">Lesotho</option>
     <option value="LR">Liberia</option>
     <option value="LY">Libyan Arab Jamahiriya</option>
     <option value="LI">Liechtenstein</option>
     <option value="LT">Lithuania</option>
     <option value="LU">Luxembourg</option>
     <option value="MO">Macau</option>
     <option value="MK">Macedonia, The Former Yugoslav Republic of</option>
     <option value="MG">Madagascar</option>
     <option value="MW">Malawi</option>
     <option value="MY">Malaysia</option>
     <option value="MV">Maldives</option>
     <option value="ML">Mali</option>
     <option value="MT">Malta</option>
     <option value="MH">Marshall Islands</option>
     <option value="MQ">Martinique</option>
     <option value="MR">Mauritania</option>
     <option value="MU">Mauritius</option>
     <option value="YT">Mayotte</option>
     <option value="MX">Mexico</option>
     <option value="FM">Micronesia, Federated States of</option>
     <option value="MD">Moldova, Republic of</option>
     <option value="MC">Monaco</option>
     <option value="MN">Mongolia</option>
     <option value="MS">Montserrat</option>
     <option value="MA">Morocco</option>
     <option value="MZ">Mozambique</option>
     <option value="MM">Myanmar</option>
     <option value="NA">Namibia</option>
     <option value="NR">Nauru</option>
     <option value="NP">Nepal</option>
     <option value="NL">Netherlands</option>
     <option value="AN">Netherlands Antilles</option>
     <option value="NC">New Caledonia</option>
     <option value="NZ">New Zealand</option>
     <option value="NI">Nicaragua</option>
     <option value="NE">Niger</option>
     <option value="NG">Nigeria</option>
     <option value="NU">Niue</option>
     <option value="NF">Norfolk Island</option>
     <option value="MP">Northern Mariana Islands</option>
     <option value="NO">Norway</option>
     <option value="OM">Oman</option>
     <option value="PK">Pakistan</option>
     <option value="PW">Palau</option>
     <option value="PA">Panama</option>
     <option value="PG">Papua New Guinea</option>
     <option value="PY">Paraguay</option>
     <option value="PE">Peru</option>
     <option value="PH">Philippines</option>
     <option value="PN">Pitcairn</option>
     <option value="PL">Poland</option>
     <option value="PT">Portugal</option>
     <option value="PR">Puerto Rico</option>
     <option value="QA">Qatar</option>
     <option value="RE">Reunion</option>
     <option value="RO">Romania</option>
     <option value="RU">Russian Federation</option>
     <option value="RW">Rwanda</option>
     <option value="KN">Saint Kitts and Nevis</option>
     <option value="LC">Saint LUCIA</option>
     <option value="VC">Saint Vincent and the Grenadines</option>
     <option value="WS">Samoa</option>
     <option value="SM">San Marino</option>
     <option value="ST">Sao Tome and Principe</option>
     <option value="SA">Saudi Arabia</option>
     <option value="SN">Senegal</option>
     <option value="SC">Seychelles</option>
     <option value="SL">Sierra Leone</option>
     <option value="SG">Singapore</option>
     <option value="SK">Slovakia (Slovak Republic)</option>
     <option value="SI">Slovenia</option>
     <option value="SB">Solomon Islands</option>
     <option value="SO">Somalia</option>
     <option value="ZA">South Africa</option>
     <option value="GS">South Georgia and the South Sandwich Islands</option>
     <option value="ES">Spain</option>
     <option value="LK">Sri Lanka</option>
     <option value="SH">St. Helena</option>
     <option value="PM">St. Pierre and Miquelon</option>
     <option value="SD">Sudan</option>
     <option value="SR">Suriname</option>
     <option value="SJ">Svalbard and Jan Mayen Islands</option>
     <option value="SZ">Swaziland</option>
     <option value="SE">Sweden</option>
     <option value="CH">Switzerland</option>
     <option value="SY">Syrian Arab Republic</option>
     <option value="TW">Taiwan, Province of China</option>
     <option value="TJ">Tajikistan</option>
     <option value="TZ">Tanzania, United Republic of</option>
     <option value="TH">Thailand</option>
     <option value="TG">Togo</option>
     <option value="TK">Tokelau</option>
     <option value="TO">Tonga</option>
     <option value="TT">Trinidad and Tobago</option>
     <option value="TN">Tunisia</option>
     <option value="TR">Turkey</option>
     <option value="TM">Turkmenistan</option>
     <option value="TC">Turks and Caicos Islands</option>
     <option value="TV">Tuvalu</option>
     <option value="UG">Uganda</option>
     <option value="UA">Ukraine</option>
     <option value="AE">United Arab Emirates</option>
     <option value="GB">United Kingdom</option>
     <option value="US">United States</option>
     <option value="UM">United States Minor Outlying Islands</option>
     <option value="UY">Uruguay</option>
     <option value="UZ">Uzbekistan</option>
     <option value="VU">Vanuatu</option>
     <option value="VE">Venezuela</option>
     <option value="VN">Viet Nam</option>
     <option value="VG">Virgin Islands (British)</option>
     <option value="VI">Virgin Islands (U.S.)</option>
     <option value="WF">Wallis and Futuna Islands</option>
     <option value="EH">Western Sahara</option>
     <option value="YE">Yemen</option>
     <option value="YU">Yugoslavia</option>
     <option value="ZM">Zambia</option>
     <option value="ZW">Zimbabwe</option>
     </select>';
   }    
}//------END CLASS
?>

 

this is my config.php

<?php 
include("classes/classlib.php");
//----HERE IS THE MAIN WEBSITE STUFF--------
$web_title = "School-Cheats.com";
$web_slogan = "We Have All The Answers!";
date_default_timezone_set('America/New_York');
//------------------------
$cms = new cms();
$cms->host = 'localhost';
$cms->username = 'moneypok_school';
$cms->password = 'pass123';
$cms->db = 'moneypok_school';
$cms->current_time = date('D,F j, Y, h:i:s A');
$cms->id = $_GET['school_id'];
$cms->c_id = $_GET['class_id'];

$cms->school = $_POST['school_name'];
$cms->country = $_POST['country'];
$cms->city = $_POST['city'];
$cms->state = $_POST['state'];
$cms->zip = $_POST['zip'];

//-------------------------
?>

this is my main file

 

<?php session_start();
include("config.php");
//connect to the database
$cms->connect();
if($_GET['new_school'] == "yes"){
   $cms->newSchool();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
   <div id="logo">
       <h1><a href="#"><?php echo $web_title; ?> </a></h1>
       <p><em><?php echo $web_slogan; ?> </em></p>
   </div>
   <hr />
   <!-- end #logo -->
   <div id="header">
       <div id="menu">
           <ul>
               <li><a href="index.php" class="first">Home</a></li>
               <li class="current_page_item"><a href="#">Blog</a></li>
               <li><a href="#">Photos</a></li>
               <li><a href="#">About</a></li>
               <li><a href="members/login.php">Login</a></li>
               <li><a href="register.php">Register</a></li>
           </ul>
       </div>
       <!-- end #menu -->
       <div id="search">
         <form method="get" action="">
               <fieldset>
               <input type="text" name="s" id="search-text" size="15" />
               <input type="submit" id="search-submit" value="Find School!" />
               </fieldset>
           </form>
       </div>
       <!-- end #search -->
   </div>
   <!-- end #header -->
   <!-- end #header-wrapper -->
   <div id="page">
   <div id="page-bgtop">
       <div id="content">
           <div class="post">
             <div class="entry">
               <p style="padding-left:250px;"><strong>Fill out the form to add a your school!</strong></p>
               <p><form action="new_school.php?new_school=yes" method="post">
               <center><table align="center" border="1">
                   <tr>
                       <td>School Name: </td><td><input type="text" name="school_name" /></td>
                   </tr>
                   <tr>
                       <td>Country: </td><td><?=$cms->country(); ?></td>
                   </tr>
                   <tr>
                       <td>City: </td><td><input type="text" name="city" /></td>
                   </tr>
                   <tr>
                       <td>State: </td><td><input type="text" name="state" /></td>
                   </tr>
                   <tr>
                       <td>Zip: </td><td><input type="text" name="zip" /></td>
                   </tr>
                   <tr>
                       <td></td><td><input type="submit" name="submit" value="Add School!" /></td>
                   </tr>
               </table></center>
               </p>
               </div>
           </div>
     </div>
       <!-- end #content -->
       <div id="sidebar">
           <ul>
               <li>
                   <h2>Lates News </h2>
                   <p>School-Cheats.com is now up and running! Tell everyone about this and lets all be lazy cheaters! </p>
               </li>
           </ul>
       </div>
       <!-- end #sidebar -->
       <div style="clear: both;"> </div>
   </div>
   </div>
   <!-- end #page -->
   <div id="footer">
       <p>Copyright (c) 2009-2010 <?php echo $web_title; ?>. All rights reserved..</p>
   </div>
   <!-- end #footer -->
</div>
</body>
</html>

 

and i don't seem to get any errors but the code doesn't seem to work either.

it doesn't insert the values into the database

Link to comment
Share on other sites

and stefan i was looking through your web design business section where you were talking about web design contracts and how they are necessary.....

 

i found some useful contracts on the web that i would like to share with you. I'm not sure if you have posted the contracts online or not but i have the following:

  • Website Preparation Worksheet

  • Website design Contract

  • Website Maintenance Contract

 

and i will be more than happy to share them with killersites. I beleive you should put them somewehre where other designers can use the templates as well. :D

Link to comment
Share on other sites

and i will be more than happy to share them with killersites. I beleive you should put them somewehre where other designers can use the templates as well.

 

Cool. I just haven't had time to post anything myself.

 

Just stick them in a zip and upload them to the forum ... we have a business of web design forum to do that.

 

Let me know when you've placed them.

 

Thanks,

 

Stefan

Link to comment
Share on other sites

i'm not sure what you mean by "try-catch block" but i do believe i understand the return piece of it from your OOP video tutorial.

 

should i do it like this??:

function newSchool(){
       $sql = mysql_query("INSERT INTO schools (school, country, city, state, zip) VALUES (" . $this->school . ',' . $this->country . ',' . $this->city . ',' . $this->state . ',' . $this->zip . ")");
        return $sql;
   }

Link to comment
Share on other sites

I found a different opensource database class file that allows me to insert, update, and delete records. i'm not sure what my error is but i am using this new class that i found and it works fine.

 

here is the class:

 

<?php
# Name: Database.class.php
# File Description: MySQL Class to allow easy and clean access to common mysql commands
# Author: ricocheting
# Web: http://www.ricocheting.com/
# Update: 2009-12-17
# Version: 2.2.2
# Copyright 2003 ricocheting.com


/*
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/



//require("config.inc.php");
//$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);


###################################################################################################
###################################################################################################
###################################################################################################
class Database {


var $server   = ""; //database server
var $user     = ""; //database login name
var $pass     = ""; //database login password
var $database = ""; //database name
var $pre      = ""; //table prefix


#######################
//internal info
var $error = "";
var $errno = 0;

//number of rows affected by SQL query
var $affected_rows = 0;

var $link_id = 0;
var $query_id = 0;


#-#############################################
# desc: constructor
function Database($server, $user, $pass, $database, $pre=''){
   $this->server=$server;
   $this->user=$user;
   $this->pass=$pass;
   $this->database=$database;
   $this->pre=$pre;
}#-#constructor()


#-#############################################
# desc: connect and select database using vars above
# Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
function connect($new_link=false) {
   $this->link_id=@mysql_connect($this->server,$this->user,$this->pass,$new_link);

   if (!$this->link_id) {//open failed
       $this->oops("Could not connect to server: <b>$this->server</b>.");
       }

   if(!@mysql_select_db($this->database, $this->link_id)) {//no database
       $this->oops("Could not open database: <b>$this->database</b>.");
       }

   // unset the data so it can't be dumped
   $this->server='';
   $this->user='';
   $this->pass='';
   $this->database='';
}#-#connect()


#-#############################################
# desc: close the connection
function close() {
   if(!@mysql_close($this->link_id)){
       $this->oops("Connection close failed.");
   }
}#-#close()


#-#############################################
# Desc: escapes characters to be mysql ready
# Param: string
# returns: string
function escape($string) {
   if(get_magic_quotes_runtime()) $string = stripslashes($string);
   return @mysql_real_escape_string($string,$this->link_id);
}#-#escape()


#-#############################################
# Desc: executes SQL query to an open connection
# Param: (MySQL query) to execute
# returns: (query_id) for fetching results etc
function query($sql) {
   // do query
   $this->query_id = @mysql_query($sql, $this->link_id);

   if (!$this->query_id) {
       $this->oops("<b>MySQL Query fail:</b> $sql");
       return 0;
   }

   $this->affected_rows = @mysql_affected_rows($this->link_id);

   return $this->query_id;
}#-#query()


#-#############################################
# desc: fetches and returns results one line at a time
# param: query_id for mysql run. if none specified, last used
# return: (array) fetched record(s)
function fetch_array($query_id=-1) {
   // retrieve row
   if ($query_id!=-1) {
       $this->query_id=$query_id;
   }

   if (isset($this->query_id)) {
       $record = @mysql_fetch_assoc($this->query_id);
   }else{
       $this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
   }

   return $record;
}#-#fetch_array()


#-#############################################
# desc: returns all the results (not one row)
# param: (MySQL query) the query to run on server
# returns: assoc array of ALL fetched results
function fetch_all_array($sql) {
   $query_id = $this->query($sql);
   $out = array();

   while ($row = $this->fetch_array($query_id, $sql)){
       $out[] = $row;
   }

   $this->free_result($query_id);
   return $out;
}#-#fetch_all_array()


#-#############################################
# desc: frees the resultset
# param: query_id for mysql run. if none specified, last used
function free_result($query_id=-1) {
   if ($query_id!=-1) {
       $this->query_id=$query_id;
   }
   if($this->query_id!=0 && !@mysql_free_result($this->query_id)) {
       $this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
   }
}#-#free_result()


#-#############################################
# desc: does a query, fetches the first row only, frees resultset
# param: (MySQL query) the query to run on server
# returns: array of fetched results
function query_first($query_string) {
   $query_id = $this->query($query_string);
   $out = $this->fetch_array($query_id);
   $this->free_result($query_id);
   return $out;
}#-#query_first()


#-#############################################
# desc: does an update query with an array
# param: table (no prefix), assoc array with data (doesn't need escaped), where condition
# returns: (query_id) for fetching results etc
function query_update($table, $data, $where='1') {
   $q="UPDATE `".$this->pre.$table."` SET ";

   foreach($data as $key=>$val) {
       if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
       elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
       else $q.= "`$key`='".$this->escape($val)."', ";
   }

   $q = rtrim($q, ', ') . ' WHERE '.$where.';';

   return $this->query($q);
}#-#query_update()


#-#############################################
# desc: does an insert query with an array
# param: table (no prefix), assoc array with data
# returns: id of inserted record, false if error
function query_insert($table, $data) {
   $q="INSERT INTO `".$this->pre.$table."` ";
   $v=''; $n='';

   foreach($data as $key=>$val) {
       $n.="`$key`, ";
       if(strtolower($val)=='null') $v.="NULL, ";
       elseif(strtolower($val)=='now()') $v.="NOW(), ";
       else $v.= "'".$this->escape($val)."', ";
   }

   $q .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";

   if($this->query($q)){
       //$this->free_result();
       return mysql_insert_id($this->link_id);
   }
   else return false;

}#-#query_insert()


#-#############################################
# desc: throw an error message
# param: [optional] any custom error to display
function oops($msg='') {
   if($this->link_id>0){
       $this->error=mysql_error($this->link_id);
       $this->errno=mysql_errno($this->link_id);
   }
   else{
       $this->error=mysql_error();
       $this->errno=mysql_errno();
   }
   ?>
       <table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
       <tr><th colspan=2>Database Error</th></tr>
       <tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
       <?php if(strlen($this->error)>0) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
       <tr><td align="right">Date:</td><td><?php echo date("l, F j, Y a\t g:i:s A"); ?></td></tr>
       <tr><td align="right">Script:</td><td><a href="<?php echo @$_SERVER['REQUEST_URI']; ?>"><?php echo @$_SERVER['REQUEST_URI']; ?></a></td></tr>
       <?php if(strlen(@$_SERVER['HTTP_REFERER'])>0) echo '<tr><td align="right">Referer:</td><td><a href="'.@$_SERVER['HTTP_REFERER'].'">'.@$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
       </table>
   <?php
}#-#oops()


}//CLASS Database
###################################################################################################

?>

 

and here is the config.inc.php file

<?php
//database server
define('DB_SERVER', "localhost");
//database login name
define('DB_USER', "moneypok_school");
//database login password
define('DB_PASS', "pass123");

//database name
define('DB_DATABASE', "moneypok_school");

//smart to define your table names also
define('TABLE_USERS', "users");
define('TABLE_SCHOOLS', "schools");
define('TABLE_CLASSES', "classes");
define('TABLE_POSTS', "posts");
define('TABLE_ADMIN_POST', "admin_posts");

?>

Link to comment
Share on other sites

Your error was probably something trivial, like a missing quotation or something - I didn't have time to debug your code for you.

 

Regardless, what you can learn from this is:

 

- have solid error trapping and reporting in your code so you can track down errors more easily.

- test your code in simple environments, again to to make it easier to track down code.

- use open source libraries to save time and headaches.

 

Stefan

Link to comment
Share on other sites

I think it's because you don't have quotes around the data fields in the sql statement. Using ',' may seem like you have the quotes, but that only creates a comma, you have to have "','"

 

function newSchool()
{
  $sql_command = "INSERT INTO schools (school, country, city, state, zip) VALUES ('" . $this->school . "','" . $this->country . "','" . $this->city . "','" . $this->state . "','" . $this->zip . "')";
// by placing this in a variable you can test with
 echo $sql_command;

 $sql = mysql_query ($sql_command);
 return $sql; 
}

Link to comment
Share on other sites

  • 3 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...