1

I just transfered my website to another hosting, but some scripts stop working, many very simple as this:

    mysql_query("INSERT INTO orders (userid,cid,tipou,cantidad,factura,producto,lote,notas,cprod,pbase,cenvio,fecha,status,cobro,ip)

VALUES ('{$_SESSION['id_usuario']}','{$cid}','{$tipou}','{$cantidad}','{$factura}','{$producto}','{$numlot}','{$notas}','{$cprod}','{$pbase}','{$cenvio}','{$fecha}','{$status}','{$cobro}','{$ip}')");

Im using php version 5.6, And tryed to enable Error Reporting, but I dont got any warnings, the query don't INSERT any data on the table.

Please apologize my english, and thanks for helping.

  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – rackemup420 Mar 17 '17 at 05:15
  • Thanks for your answer, i just bought some books to learn, but all of them uses mysql_ :( – Guillermo Esquivel Mar 17 '17 at 05:17
  • @GuillermoEsquivel.for new updates please learn online tutorials.most of the books outdated since programming changes day by day – Vision Coderz Mar 17 '17 at 05:17
  • Anyway, the script works if I remove variables ($) and use specific data entered by myself. – Guillermo Esquivel Mar 17 '17 at 05:18
  • @GuillermoEsquivel Generally speaking books are completely useless because by the time they get printed, half of their material becomes obsolete. Online tutorials, official mailing lists and various tech blogs will work a lot better. – Dimi Mar 17 '17 at 05:18
  • I have a recommendation. Don't use PHP. – Sumner Evans Mar 17 '17 at 05:18
  • well, the bad news are: I just developed a whole CRM and SALES website for my own business using PHP and mysql_ WANNA DIE. – Guillermo Esquivel Mar 17 '17 at 05:21
  • if you follow the MVCl structure and prepared statements you are golden and its super easy to code your website – rackemup420 Mar 17 '17 at 05:24
  • actually I dont know why I bought MYSQL and PHP books, existing php.net, but anyway, thanks to all for helping Me! have a good night. – Guillermo Esquivel Mar 17 '17 at 05:32

2 Answers2

1
This is how i set it up pretty much:

connect.php:

<?php

define('DB_HOSTNAME', 'HOSTNAME');
define('DB_USERNAME', 'USERNAME');
define('DB_PASSWORD', 'PASSWORD');
define('DB_DATABASE', 'DATABASE');

function dataQuery($query, $params) {
$queryType = explode(' ', $query);

// establish database connection
try {
    $dbh = new PDO('mysql:host='.DB_HOSTNAME.';dbname='.DB_DATABASE, DB_USERNAME, DB_PASSWORD);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    echo $e->getMessage();
    $errorCode = $e->getCode();
}

// run query
try {
    $queryResults = $dbh->prepare($query);
    $queryResults->execute($params);
    if($queryResults != null && 'SELECT' == $queryType[0]) {
        $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
    $queryResults = null; // first of the two steps to properly close
    $dbh = null; // second step to close the connection
}
catch(PDOException $e) {
    $errorMsg = $e->getMessage();
    echo $errorMsg;
}
}

?>

Then i just use functions to do the work:

<?php

    function functionNameHere() {
        $query = "INSERT INTO orders (userid,cid,tipou,cantidad,factura,producto,lote,notas,cprod,pbase,cenvio,fecha,status,cobro,ip) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $params = array($_SESSION['id_usuario'],etc...);
        dataQuery($query,$params);
    }

?>

then just call <?php functionNameHere(); ?>

rackemup420
  • 1,600
  • 2
  • 15
  • 37
0

Created this:

$sessid = $_SESSION['id_usuario'];
if (!$mysqli->query("INSERT INTO orders (userid,cid,tipou,cantidad,factura,producto,lote,notas,cprod,pbase,cenvio,fecha,status,cobro,ip)

VALUES ('".$sessid."','".$cid."','".$tipou."','".$cantidad."','".$factura."','".$producto."','".$numlot."','".$notas."','".$cprod."','".$pbase."','".$cenvio."','".$fecha."','".$status."','".$cobro."','".$ip."')")) {
    echo "Falló la Insersión de Datos: (" . $mysqli->errno . ") " . $mysqli->error;
} else {echo"Datos Insertados";}

Working on localhost, but not in my server.