does anyone know how I can put a date as the name of a column in phpmyadmin?

0

It turns out that I have a php with data and one of these is a date and I want to put it as the name of a column but I get an error, they know how I can do it so that I do not get that error, this is the php

<?php
require('../../conexion.php');

$fecha=$_POST['fecha'];
$id=$_POST['id'];
$asistencia=$_POST['asistencia'];

$sql="ALTER TABLE asistencias ADD $fecha varchar(15) NOT NULL";
$result=$conexion->query($sql);

if($result)
{
    $sql2="UPDATE asistencias "
        . "SET "
        . "$fecha='$asistencia' "
        . "WHERE Id=$id";
    $result2=$conexion->query($sql2);
    if($result2)
    {

    }
    else
    {
        echo 'error';
        echo $sql2;
    }
}
else
{
    echo 'error';
    echo $sql;
}
?>
    
asked by Sebastian Zarate 18.04.2017 в 04:10
source

1 answer

1

You can check the documentation about it Database names, tables , indexes, columns and alias .

For specific column names you can use any character with a limit of 64 characters of the current character set.

The designer is free to establish the names that he considers appropriate for the columns, even though my recommendation is to follow some minimum rules:

  • Use alphanumeric characters, without strange characters such as ñ, tildes or silimales.
  • Limit names to less than 32 characters.
  • Use the underscore (_) to separate words.
  • Always use lowercase words, some engines convert it automatically.
  • The names of the tables should be in the plural and the names of the columns in the singular.
  • In a table, place the primary key first followed by the foreign keys.

Your statement does not work because you must quote the name of the table because it may contain spaces or other characters that make the query invalid.

This is how it should work:

$sql="ALTER TABLE asistencias ADD '$fecha' varchar(15) NOT NULL";

However, I can not recommend that you follow this path, as you have been told what you want to do is a concept error. Not only why your table will grow in width (not recommended), think about how complex it can be to make queries to that table.

On the other hand, choose the option you choose, you should minimally prevent the SQL injection .

    
answered by 18.04.2017 в 15:41