Enter values to the database

0

I have my database created, in it a table with the fields, ID, VALUE, VALUE2, TIME, I have my PHP file that I enter the values (VALUE AND VALUE2), which is this:

<?php
    $conexion = mysql_connect("localhost", "usuarioservidor", "1234");
    mysql_select_db("bdservidor",$conexion);
    mysql_query("INSERT INTOtablaservidor(valor,valor2) VALUES ('" . $_GET['valor'] . "','" . $_GET['valor2'] . "')", $conexion);
 ?>

perfect all right there, I inserted the data correctly, I assigned the ID and TIME, the question is as follows, when I enter only one data for example be VALOR or VALOR2 , the other data assigns me a 0 , how can I do so that the other value stays the previous one, and configure in the options so that I put a NULL , but it does not work for me, because I am extracting information from that database with this code:

<?php
$dbhost = 'localhost';
$dbuser = 'usuarioservidor';
$dbpass = '1234';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}

$sql = 'SELECT ID, VALOR, VALOR2, TIEMPO FROM tablaservidor ORDER BY id DESC limit 1' ;
mysql_select_db('bdservidor');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
  die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
  echo 
      " id={$row[0]}   \n ".
     "valor={$row[1]}  \n ".
     "valor2={$row[2]} \n ".
     "tiempo={$row[3]} \n ".
     "";
}

mysql_free_result($retval);
//echo " Datos leidos de la base de datos MYSQL\n";

mysql_close($conn);
?>' 

when I extract the values and only assign a value to VALUE and not to VALOR2 shows me this:

I need the other data to keep the last value even if I assign a single value to any of the 2, is that I'm turning on some LEDs by this I do not need to be assigned neither NULL , nor 0, I need to I keep the last value even though I only give you one piece of information, I appreciate it if someone can help me.

    
asked by Staz 06.04.2017 в 20:08
source

1 answer

0

First you have to get the last values, then you have to verify which one is missing and then add it to the database.

 <?php
     $conexion = mysql_connect("localhost", "usuarioservidor", "1234");
     mysql_select_db("bdservidor",$conexion);
        if (!isset($_GET['valor'],$_GET['valor2'])) {
            $sql = 'SELECT VALOR, VALOR2 FROM tablaservidor ORDER BY id DESC limit 1' ;
            mysql_select_db('bdservidor');
            $retval = mysql_query( $sql, $conexion );

            if(! $retval ) {
              die('Could not get data: ' . mysql_error());
            }

            while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
                if (!isset($_GET['valor'])){
                    $valor={$row[0]};
                }else{
                    $valor=$_GET['valor'];
                }
                if (!isset($_GET['valor2'])){
                    $valor2={$row[1]};
                }else{
                    $valor2=$_GET['valor2'];
                }
            }
        } else {
            $valor=$_GET['valor'];
            $valor2=$_GET['valor2'];

        }       
        mysql_query("INSERT INTOtablaservidor(valor,valor2) VALUES ('" . $valor . "','" . valor2 . "')", $conexion);
?>
    
answered by 06.04.2017 в 20:42