[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

PHP script

Code works, but it doesn't add data to database as it's the main goal.
<?php
   include "db.inc";
   include "prodlist.class";
?>
<html>
   <head>
      <title>
         Add a Simgle Product
      </title>
   </head>

   <body>

      <b><font color="#000099"><font size=+2>Flamingo  Add Product  Form</font></font></b>&nbsp;&nbsp;&nbsp;&nbsp;<img SRC="pinkflamingo.gif" NOSAVE height=85 width=61>
      <br>
      <br>


<?php



   $product = new ProdList;

$code =  $_POST["prodcode"];
   $name =  $_POST["prodname"];
   $desc =  $_POST["proddesc"];
   $price = $_POST["prodprice"];
 



	$product ->setProdCode($_POST["prodcode"]); //get product code from posted form
	$product ->setProdName($_POST["prodname"]);   //get product Name code from posted form
	$product ->setProdDesc($_POST["proddesc"]);  //get product Description code from posted form
	$product ->setProdPrice($_POST["prodprice"]);  //get product price from posted form
	if (!$product->getRecord($code, $name, $desc, $price))

  {
      echo("<pre>");
      

      echo("</pre>");
      echo("<p><a href='main.html'>Return to Main</a>");
}
else{
	   echo("<p> The product with code " . $code . " exists in the products table !!");
	   echo("<p><a href='addProduct.html'>Please Try Again</a>");
	   die("<br><br>");	
   }
 

?>

      <p> The Product details were successfully added, <a href="main.html">Continue</a>

   </body>
</html>

Open in new window

0
antatiana
Asked:
antatiana
  • 8
  • 4
  • 3
  • +2
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
You have to look at function,  getRecord();

Can u post the code for this function?
0
 
Loganathan NatarajanLAMP DeveloperCommented:
if you put this,

 if (!$product->getRecord($code, $name, $desc, $price))

  {
      echo "ERROR";
}
else{
           echo("<p> The product with code " . $code . " exists in the products table !!");
           echo("<p><a href='addProduct.html'>Please Try Again</a>");
           die("<br><br>");    
   }
0
 
antatianaAuthor Commented:
no, it doesn't help
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
antatianaAuthor Commented:
how I can post to function getRecord()?
0
 
Loganathan NatarajanLAMP DeveloperCommented:
You already passing the values like $code, $name, $desc, $price

you can get these variable values (posted) on the function ...

just echo $code;

getRecord($code, $name, $desc, $price) {

echo $code;


}
0
 
antatianaAuthor Commented:
but I need to add data to database and see them at prod list
0
 
Loganathan NatarajanLAMP DeveloperCommented:
You can construct insert query and execute on the function itself,

ref. Insert
0
 
Ray PaseurCommented:
Code works, but it doesn't add data to database as it's the main goal.

Car works, but it doesn't start when I turn the key and step on the gas...

In other words, it does not work.  This is not really a question with an answer; it is a requirement for application development and for something like that you might want to turn to ELance or a similar source and hire a professional developer.   Many of the experts at EE have "hire me" buttons on their profiles, so you might also look at the profiles of the experts and consider who might be able to help you get the code working.

But before you do that, here are some things that any professional will need.  If you get this together before you put out a call for help, you will get better and faster results at much lower cost!

1. The CREATE TABLE statements for your data base
2. The test data set you are using to exercise the code
3. The written explanation of the expected outputs from the tests
4. The full PHP code of all the non-native functions and classes (example: What is this? $product = new ProdList;)
5. The failing working script and the evidence that you have to show that it is failing
6. The URL of the scripts, so they can be tested over the internet (if the scripts and data are not on a public web server, please put them on a public server before asking a professional to help; it will save you a lot of money if you have a development environment).

Armed with that information your professional developer will be able to get you a very good solution very quickly.  If any of that information is missing, expect your costs and the time requirements to rise very, very sharply.
0
 
antatianaAuthor Commented:
Thank you very much for your comments, you are really right in your advice. I have tried to contact experts via hire button and hope it will help me in my problem which needs very urgent solution.
0
 
Marco GasiFreelancerCommented:
In code you have shown simply doesn't exist any line which add data to a database: there is only echo statements like
echo("<pre>");
      

      echo("</pre>");
      echo("<p><a href='main.html'>Return to Main</a>");

Open in new window


(<pre> and </pre> are totally unneeded since nothing is echoed within them)

or like
echo("<p> The product with code " . $code . " exists in the products table !!");
	   echo("<p><a href='addProduct.html'>Please Try Again</a>");
	   die("<br><br>");

Open in new window


The only function is getRecord(): I think you find it in 'prodlist.class' (which I would rename as prodlist.class.php). Look there and copy and paste here the getRecord code.

But, if that function has been correctly named, I suppose it not post any to the database and it only get data from some source, probably the database itself. So, my opinion is that code to post data to database is totally missing.

If you are trying to write this application by yourself and if you don't know php, I totally agree with Ray suggestion. If you want to learn Php and you can wait for make your app, then you can start with this book: http://www.sitepoint.com/books/phpmysql4/.

Finally, code to post data in database looks like this:

$sql = "INSERT INTO table (code, name, desc, price) VALUES ('$code', '$name', '$desc', '$price')";
mysql_query($sql);

So your code should look like this:
 
<?php
   include "db.inc";
   include "prodlist.class";
?>
<html>
   <head>
      <title>
         Add a Simgle Product
      </title>
   </head>

   <body>

      <b><font color="#000099"><font size=+2>Flamingo  Add Product  Form</font></font></b>&nbsp;&nbsp;&nbsp;&nbsp;<img SRC="pinkflamingo.gif" NOSAVE height=85 width=61>
      <br>
      <br>


<?php



   $product = new ProdList;

$code =  $_POST["prodcode"];
   $name =  $_POST["prodname"];
   $desc =  $_POST["proddesc"];
   $price = $_POST["prodprice"];
 



	$product ->setProdCode($_POST["prodcode"]); //get product code from posted form
	$product ->setProdName($_POST["prodname"]);   //get product Name code from posted form
	$product ->setProdDesc($_POST["proddesc"]);  //get product Description code from posted form
	$product ->setProdPrice($_POST["prodprice"]);  //get product price from posted form
	if (!$product->getRecord($code, $name, $desc, $price))

  {

      $sql = "INSERT INTO table (code, name, desc, price) VALUES ('$code', '$name', '$desc', '$price')";
      mysql_query($sql);
      echo("<p><a href='main.html'>Return to Main</a>");
}
else{
	   echo("<p> The product with code " . $code . " exists in the products table !!");
	   echo("<p><a href='addProduct.html'>Please Try Again</a>");
	   die("<br><br>");	
   }
 

?>

      <p> The Product details were successfully added, <a href="main.html">Continue</a>

   </body>
</html>

Open in new window


Cheers
0
 
antatianaAuthor Commented:
code for new class ProdList is here
<?php

class ProdList
{
    var $dbname = "flamingo";
    var $tablename = "prodList";

    var $prodCode      = "";
    var $prodName     = "";
    var $prodDesc      = "";
    var $prodPrice      = 0;

    function ProdList() {   
   }

    function getRecord($code)  {
        $sqlQuery = "select * from " . $this->tablename . " WHERE prodCode='" . $code . "'";

        global $dbConn;
        if (!dbConnect($this->dbname)) return FALSE;
       $dbResult = odbc_exec($dbConn,$sqlQuery);

        if (odbc_fetch_row($dbResult)) {
            $this->prodCode     = odbc_result ($dbResult, "prodCode");
            $this->prodName    = odbc_result ($dbResult, "prodName");
            $this->prodDesc     = odbc_result ($dbResult, "prodDesc");
            $this->prodPrice     = odbc_result ($dbResult, "prodPrice");
            return true;
        } 
        else {
            return false;
        }
    }

   function updateRecord() {

        $sqlQuery = "update " . $this->tablename . " set ";
        $sqlQuery .= "prodName='" . $this->prodName . "', ";
        $sqlQuery .= "prodDesc='" . $this->prodDesc . "', ";
        $sqlQuery .= "prodPrice=" . $this->prodPrice . " ";
        $sqlQuery .= "where prodCode='" . $this->prodCode . "'";

        global $dbConn;
        if (!dbConnect($this->dbname)) return false;
       $dbResult = odbc_exec($dbConn,$sqlQuery);
        if (!$dbResult) {
           return false;
        }
        else {
           return true;
        }
   }

   // --------------------------------------------------------------------------------------------------

    // Standard get and Set methods

    function getProdCode() {
        return $this->prodCode;
    }
    function setProdCode($code) {
        $this->prodCode = $code;
    }

    function getProdName() {
        return $this->prodName;
    }
    function setProdName($name) {
        $this->prodName = $name;
    }

    function getProdDesc() {
        return $this->prodDesc;
    }
    function setProdDesc($desc) {
        $this->prodDesc = $desc;
    }

    function getProdPrice() {
        return $this->prodPrice;
    }
    function setProdPrice($price) {
        $this->prodPrice = $price;
    }
}

?>

Open in new window

0
 
antatianaAuthor Commented:
Thank you marquis!
But it also doesn't work.
0
 
Ray PaseurCommented:
To add a little bit to the very good advice that marqusG wrote, you might want to test the return values from the functions, especially the data base functions.   MySQL is not a black box; it can and will fail for reasons that are not always in your control, and your programming needs to handle these errors, or at least bring them to your attention for manual intervention.

Instead of just this:

mysql_query($sql);

Use something more like this:

$res = mysql_query($sql);
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}

Here are some man pages that describe the basic things about the interaction of PHP and MySQL.  I often find the user-contributed notes to be the best part of the online manual!

 http://php.net/manual/en/ref.mysql.php
 http://php.net/manual/en/mysql.installation.php
 http://php.net/manual/en/function.mysql-connect.php
 http://php.net/manual/en/function.mysql-select-db.php
 http://php.net/manual/en/function.mysql-real-escape-string.php
 http://php.net/manual/en/function.mysql-query.php
 http://php.net/manual/en/function.mysql-errno.php
 http://php.net/manual/en/function.mysql-error.php
 http://php.net/manual/en/function.mysql-num-rows.php
 http://php.net/manual/en/function.mysql-fetch-assoc.php
 http://php.net/manual/en/function.mysql-fetch-array.php
 http://php.net/manual/en/function.mysql-insert-id.php

0
 
antatianaAuthor Commented:
Ray, it also doesn't work. I have tried to connect you via hire button, may be if you have time, contact me to help with this challenge in php scrip.
0
 
Ray PaseurCommented:
I would like to help, but I am fully busy with other work until the end of this month.  There are many others who can help.  My best advice to you is to use lots of data visualization and have lots of well-organized test data.  The PHP function var_dump() is your friend.
0
 
Scott MadeiraCommented:
Ray is correct, you should get a professional developer to help you.  It will make you a much happier person.  

A quick glance at your ProdList class shows that you can only update an existing record or get an existing record.  That class has no ability to insert a new record into your database.  

Step one would be to fix that class so that it can create records (and delete while you are at it.)  

Step 2 would be to update your script to use the newly created insert function to put the data into your database.
0
 
antatianaAuthor Commented:
Thank you all for your help!
Yes I understand I need help of professional expert. If someone can help me, please contact me at antatianka6 at gmail dot com
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now