Jump to content

dmcglone

Members
  • Posts

    54
  • Joined

  • Last visited

About dmcglone

  • Birthday 10/12/1972

Profile Information

  • Gender
    Male
  • Location
    Columbus, Ohio

dmcglone's Achievements

Member

Member (2/5)

3

Reputation

  1. Well I'll be darned! I thought I had marked that column as unique the other day, but it refused to make the changes because there were duplicates and I didn't realize it.. It's working correctly now... Thank you for the heads up. took me a while to figure this one out, but I learned a whole lot. :-)
  2. Haha Okie doke. I think I see what's happening now. I think the function is evaluating only if a row is inserted which will always be true, because I don't have any checks to compare whether the same vin already exists in the DB. Once I write some code to check whether the vin already exists, then I will then be able to stop the insertion if it already exist. :-) Thoughts anyone?
  3. last post was a little pre-mature. I found my answer on how to use mysql_affect_rows. Here's what I changed the function to, but the problem of adding duplicates is still there, but now I think I see why. function insertVin($vin) { //Trim and escape the value $vinValue = mysql_real_escape_string(trim($vin)); //Verify value exists if(empty($vinValue)) { return false; } //Attempt to insert the record $query = "INSERT IGNORE INTO inventory (vin) VALUES ('$vinValue')"; $result = mysqli_query($this->conn, $query); $affected_rows = mysqli_affected_rows($this->conn); echo "total # of rows affect by this insert " . $affected_rows . "<p>"; //Check if the record was inserted if($affected_rows>0) { return "added vin # {$vin}"; } else { return "{$vin} is already in the database"; } } }
  4. Hello guys. I finally got some time to play around with psycho's example. I have only slightly modified it to see if what I could do. The only problem is, if the vin number does exist in the db, it's still adding that number instead of executing the else condition and I'm wondering what could be causing it. //sql_functions.php class Connection { //Create the connection to mysql public function __construct() { $this->conn= mysqli_connect(DB_HOST,DB_USER,DB_PASS); if(!$this->conn){ die ("Cannot connect to the database " .DB_NAME); } //after making a connection to mysql, we then select which database we want to use. mysqli_select_db($this->conn, DB_NAME); if(mysql_error()){ echo "Cannot find the database table "; exit(); }else { echo "Database " .DB_NAME. " selected <p />"; } } function insertVin($vin) { //Trim and escape the value $vinValue = mysql_real_escape_string(trim($vin)); //Verify value exists if(empty($vinValue)) { return false; } //Attempt to insert the record $query = "INSERT IGNORE INTO inventory (vin) VALUES ('$vinValue')"; $result = mysqli_query($this->conn, $query); //Check if the record was inserted if(mysqli_affected_rows($this->conn)>0) { return "added vin # {$vin}"; } else { return "{$vin} is already in the database"; } } } //test_sql_functions.php require_once "errorReporting.inc.php"; require_once "sql_functions.php"; $connection = new Connection(); $isertVinResult = $connection->insertVin($_POST['vin']); if(!$isertVinResult) { echo "There was a problem attempting to insert the vin '{$_POST['vin']}'"; } else { echo "$isertVinResult correctly"; }
  5. Thanks guys for the responses. I'm about to play around with the examples and try to understand them. Psycho, although this is completely for my own learning, aside from giving an example, thank you for the explanations. I think I got more excited over them than the code itself.
  6. Hmmm, I spent all day Tuesday trying it the way you suggest and for some reason I wasn't getting the correct results. I finally changed it around last night to something I understood better and I wasn't happy with myself knowing I should have been able to do it in less steps. I think one reason for my confusion is probably because this is the first time I've ever sat down and tried to use mysqli and I'm not comfortable with it yet. Anyway later today I'm going to write it again like you suggest and maybe this time around I'll spot where I was going wrong the first time. Blessings David M.
  7. I don't think I fully understand MySQL or PHP for that matter. LOL but anyhow, I have written a function that will check to see if a value is in the db and if not, insert the record and if not, then don't and I believe there is probably a much easier way to do this without having to pull out all these steps I have. Can anyone educate me here a little bit and show me how they would accomplish this task in a much shorter or simpler way? Here's the code I have written: function insert(){ $this->vin = $_POST['vin']; $result = mysqli_query($this->myconn, "SELECT * FROM inventory"); while ($row = mysqli_fetch_array($result)){ $vins = $row[7]; } if($this->vin != $vins ){ $insert = "INSERT INTO inventory (vin) VALUES ('$_POST[vin]')"; if(!mysqli_query($this->myconn, $insert)){ die('Error: ' . mysqli_error($this->myconn)); } echo "added vin # " . $this->vin; } else { echo $this->vin . " is already in the database"; } } Blessings David M.
  8. I took a break and played a couple hours of basketball and came back and got things working the way I intended. It was the way I was working with my arrays like psycho said. So thank for pointing me in the right direction. Now I'm going to try my hand at putting them into a db as AbraCadaver suggested.
  9. Thanks abracadaver, This is just practice using smarty and I think I've completely messed this one up. My earlier question turned out to be completely the wrong question and the reason I haven't slapped this stuff into a db is because of my lack of knowledge using smarty. Once I familiarized myself better with smarty I was then going to adapt it to a db and see how far I could get. I'm also burnt out, so that's not helping much right now. :-/
  10. Thought I was pretty good with this stuff until now. What I'm doing here is practising with smarty and I'm trying to take these array's and assign them a name such as page 1, page 2, page 3 for the purpose of displaying them in a menu, but I can't change the numbers in the array because they are used in the URL to pull the correct data from a db for that specific page. $menu = array ( '1' => 'page1.tpl', '2' => 'page2.tpl', '3' => 'page3.tpl' ); I've tried some variations but failed miserably. So, with this current code my menu on the page looks like this: page1.tpl page2.tpl page3.tpl but I'm sure anyone can see that I don't want the .tpl to show in the menu. Blessings David M.
  11. HAHAHAHAHA I forgot the curly brackets on my while loop. I was thinking the one that closed the function was the one for the while.
  12. What's happing here is I can't seem to use one these two lines instead of both at the same time: $this->rName = $row['name']; $this->rDescription = $row['description']; Here is the class that uses this code: class BlankPage { public $rName; public $rDescription; function __construct() { $p = $_GET['p']; $sql = "SELECT name, description FROM page WHERE page_id = $p"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) $this->rName = $row['name']; $this->rDescription = $row['description']; } I'm lost. Wouldn't this be the same as using: $rName = $row['name']; $rDescription = $row['description']; echo $rName; echo $rDescription;
  13. Thanks trq, that did the trick. I found quite a few other stuff that has changed also.
  14. I'm trying to extend Smarty's Class here and I keep getting this error and can't figure out why. I've used this successfully in the past, but I'm not sure if it was PHP 5. Fatal error: Uncaught exception 'SmartyException' with message 'PHP5 requires you to call __construct() instead of Smarty()' in /media/www/login/Smarty/libs/sysplugins/smarty_internal_templatebase.php:803 Stack trace: #0 /media/www/login/include/setup_smarty.php(14): Smarty_Internal_TemplateBase->__call('Smarty', Array) #1 /media/www/login/include/setup_smarty.php(14): Page->Smarty() #2 /media/www/login/index.php(7): Page->__construct() #3 {main} thrown in /media/www/login/Smarty/libs/sysplugins/smarty_internal_templatebase.php on line 803 here's the code that seems to be at fault: class Page extends Smarty { // constructor function __construct() { // Call Smarty's constructor $this->Smarty(); // Change the default template directories $this->template_dir = TEMPLATE_DIR; $this->compile_dir = COMPILE_DIR; $this->config_dir = CONFIG_DIR; $this->plugins_dir[0] = SMARTY_DIR . '/login/Smarty/libs/plugins'; $this->plugins_dir[1] = SITE_ROOT . "/login/Smarty/libs/plugins"; } } Does anyone know if Smarty has changed with PHP 5? I think that is the culprit, but I'm not sure. Thanks David M.
×
×
  • 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.