-2

I am trying to do something with INI and mysql for UCP. Now when I want to check if row exists then UPDATE the row or if it not exist just create new row. But something is going wrong here.. I do a research but I cant find how to fix this thing.

$query = mysql_query("SELECT * FROM `podatoci` WHERE `Ime` = 'Ile_Popivanov'");
if($query) {
   if(mysql_num_rows($query) == 1) 
       $sql = "UPDATE `podatoci` SET `Pari` = 123 WHERE `Ime` = 'Ile_Popivanov'";
   else 
       $sql = "INSERT INTO `podatoci`(`Pari`, `Ime`) VALUES (123456, 'Ile_Popivanov')";
} 
else 
    echo 'Nekoja greska';
aynber
  • 22,380
  • 8
  • 50
  • 63
Ile Popivanov
  • 95
  • 1
  • 1
  • 7
  • 2
    Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Jan 04 '16 at 14:46
  • 1
    What error messages do you get? – Shaun Jan 04 '16 at 14:46
  • 1
    What version of PHP is the server running? Because `mysql_*` functions are deprecated. – Geoff Atkins Jan 04 '16 at 14:48
  • 1
    First of since the release of PHP 7.0 I am asking this first. Are you using PHP 7.0 or higher? if so there is your problem. `mysql_*` functions have been deleted as of PHP 7.0 – BRoebie Jan 04 '16 at 14:48
  • 2
    and you're also not executing your last 2 queries. You're only assigning variables for them. Plus, you should be using proper bracing. We also have no idea which MySQL API you are using to connect with. Too many things wrong here. – Funk Forty Niner Jan 04 '16 at 14:50
  • I think its 5.3, but iam not sure. – Ile Popivanov Jan 04 '16 at 14:54
  • 1
    Oke check with `phpinfo();` put that in your code and see. Then we can rule out that. But still update your code because you cannot handle user input safely(correct me if I am wrong I know this is not the only reason to update but in my opinion the most important) with `mysql_*` use [`mysqli_*`](http://php.net/manual/en/mysqli.prepare.php) or [PDO](http://php.net/manual/en/pdo.prepare.php) instead. Also use [prepared statements](http://stackoverflow.com/a/60496/5396496). – BRoebie Jan 04 '16 at 14:58

2 Answers2

0

Wouldn't be easier if you alter your table and add unique key to the Ime field

ALTER TABLE .`podatoci` ADD UNIQUE INDEX `Ime_key` (`Ime` ASC);

and then instead of checking if the row exists you simply do:

INSERT IGNORE INTO `podatoci`(`Pari`, `Ime`) VALUES (123456, 'Ile_Popivanov')

or

INSERT INTO `podatoci`(`Pari`, `Ime`) VALUES (123456, 'Ile_Popivanov') ON DUPLICATE KEY UPDATE Pari=123;

As far as the error It might be due to no connection to the mysql server. And I would recommend:

  1. Check ur connection (simply where you do the mysql_connect() try doing mysql_connect($hostname, $username, $password) or die(mysql_error()); might give you a hint)

  2. Check ur PHP version (mysql_* are deprecated since php 5.5 and removed in php7).

0

try this a little example

<?php
function Conect()
{
    if (!($link=mysql_connect("localhost","user","password")))
    {
        echo "error to connect to database.";
        exit();
    }
    if (!mysql_select_db("database_name",$link))
    {
        echo "Error to select database.";
        exit();
    }
    return $link;
}

$link=Conect(); 
$query="SELECT * FROM `podatoci` WHERE `Ime` = 'Ile_Popivanov'";
$action =mysql_query($query,$link);

if (!empty($action))
{
    //if exist update
    $query="UPDATE `podatoci` SET `Pari` = 123 WHERE `Ime` = 'Ile_Popivanov'";
    $action =mysql_query($query,$link);


    if (!$action) {
       die('has not update: ' . mysql_error());
     ?> <font color="blue" face="arial" size="4">was not update</font> <?php

     }

     ?> <font color="blue" face="arial" size="4">was update</font> <?php

     mysql_close($link);

}else{
    //If not exist insert
    $query="INSERT INTO `podatoci`(`Pari`, `Ime`) VALUES (123456, 'Ile_Popivanov')";
    $action =mysql_query($query,$link);

    if (!$action) {
       die('has not insert: ' . mysql_error());
     ?> <font color="blue" face="arial" size="4">was'nt' insert</font> <?php

     }

     ?> <font color="blue" face="arial" size="4">was insert</font> <?php

     mysql_close($link);
}//end if

and good luck

BlackHack123
  • 349
  • 1
  • 10