registering php data in SQL server database 2008

Asked

Viewed 788 times

0

I have a question... I don’t know if I’m putting the Cód right, I’m a beginner in php and I need a help... I already managed to make the form I have to write in Mysql ... but now I’m working on an SQL Server and I can’t get it to record the data... I don’t know if something is missing ... I don’t know... but I needed a help... come on...

conex.php

<?
$con = mssql_connect("000.000.000.00,1111", "user", "senha");
mssql_select_db("BANCO",$con);
?>

That’s the connection I’m making ... until then it’s quiet n of error ...

this is the file that records:

writes.php

<?php require_once('Connections/conex.php'); ?>
<?php
$CPF =                       $_POST['CPF'];
...
$TITULO_3 =                  $_POST['TITULO_3'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "cpf-form")) {
  $insertSQL = sprintf("INSERT INTO BASE_EY (
  CPF,
  ...
  TITULO_3) VALUES (
  $CPF,
  ...
  $TITULO_3)");

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>

and that’s the form...

php form.

<form action="testego.php" id="camposform" method="post" name="cpf-form" target="_self">
      <div class="medium-8 columns">
        <label for="NOME">Nome<span class="red-red">*</span></label>
        <input name="NOME" id="NOME" value="" type="text" required="required">
      </div>
      <div class="medium-4 columns">
        <label for="SEXO">Sexo<span class="red-red">*</span></label>
        <select name="SEXO" id="SEXO" value="" required="required">
          <option selected="selected" disabled="disable">-- Selecione --</option>
          <option>MASCULINO</option>
          <option>FEMININO</option>
        </select>
      </div>

...

      <div class="medium-2 columns">
        <label for="ANO_GRADUACAO">Ano da Graduação<span class="red-red">*</span></label>
        <input name="ANO_GRADUACAO" id="ANO_GRADUACAO" value="" type="text" required="required">
      </div>
      <div class="div-fix"><input type="submit" class="button medium-4 columns clearfix"></div>
       <input type="hidden" name="MM_insert" value="cpf-form" />
    </form>

No error appears Cód goes straight as if everything is ok... but when I check the bank ... NOTHING... I don’t know where I’m going wrong... does anyone give me a light?? Plz :D

--------------------------------------------------- V-Edited V-V ---------------------------------------------------

I made some changes to the Cód. as per the instructions ... but it didn’t go very well... happened the same thing jumps straight as if he had registered but n cadastra... Cod was like this...

Connection file

conex.php

<?
$con = mssql_connect("000.000.000.00,5033", "user", "senha");
mssql_select_db("banco",$con);
if( $con === false )
      { die( FormatErrors( sqlsrv_errors() ) ); }
?>

cadastra.php

<?php require_once('Connections/conex.php'); ?>
<?php
$CPF =                       $_POST['CPF'];
...
$TITULO_3 =                  $_POST['TITULO_3']; //crm 02 OBS

  $insertSQL = mssql_execute("INSERT INTO BASE_EY (
  CPF,
  ...
  TITULO_3) VALUES (
  $CPF,
  ...
  $TITULO_3)");

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header("Location: $insertGoTo");
mssql_free_result($insertSQL)
?>

Any more lights out there?? rsrs

  • 4

    In your code I didn’t see any mssql_query() which is responsible for executing the query. I’m not sure if the mssql_* functions work with sql 2008, you can use the sqlsrv(example of sqlsrv connection) or PDO

  • 2

    +1 in relation to the PDO recommendation. An article explaining the basics of its use http://www.diogomatheus.com.br/blog/php/trabalhoando-compdo-no-php/

  • OK I’ll do the tests tomorrow! You’re beasts!

  • 1

    Exchange the mssql_execute() for: mssql_query('INSERT ....) or die(mssql_get_last_message());. Comment on this line header("Location: $insertGoTo"); this will avoid redirecting so if you have any error it is displayed.

  • AEEEEE HAHA! Vlw friend! That’s right! mssql_query('INSERT ....) or die(mssql_get_last_message()); he presented the message that the table "ID" was not accepting the field NULL... ai modified it to Auto-increment and funfouuu... @lost you should put as the answer! : D Thank you all ... Now I know a little more about SQL ... in this part I won’t miss more rsrs... : D

2 answers

1

To insert data into SQLSRV, it is more boring than Mysql.

Here is an example of how SQLSRV should be used:

$sql = "INSERT INTO [RDO].[dbo].[funcionarios] (usuario, email, senha) VALUES (?,?,?)";

$params = array($nome, $email, $senha);

$stmt = @sqlsrv_query( $conn, $sql, $params);

1


In your code failed to call the function mssql_query() who is responsible for executing the query. To resolve Add this line:

mssql_query('INSERT INTO ....') or die(mssql_get_last_message());

When working with the database functions (mssql_, mysql_, pg_, etc) remember to call the function that displays the error in the case mssql_get_last_message(), in new projects in preference to PDO or sqlsrv

  • Tks man! Running right here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.