Page 1 of 1

Inserting variable data into a SQL Server table

Posted: Tue Sep 30, 2008 9:27 am
by bendertez
Hello

I'm trying to insert data from a variable in to a table in SQL Server 2005.

So far my query is this which runs ok but doesn't give me the desired results:

$dsn="Server_Test";
$username="Test";
$password="srvtest";
$sqlconn=odbc_connect($dsn,$username,$password);

global $sqlconn, $stuCode, $stuFnm1, $stuSurn, $stuCount, $sql1;
$sql="SELECT top (5) STUCODE, STUFNM1, STUSURN FROM WARN_VIEW";
$row=odbc_exec($sqlconn, $sql);
while($stuCount=odbc_fetch_row($row)) {

$stuCode=odbc_result($row,'STUCODE');
$stuFnm1=odbc_result($row,'STUFNM1');
$stuSurn=odbc_result($row,'STUSURN');
$stuCount++;
}

{
$sql1="INSERT INTO DIS_WARN(SDW_CODE) VALUES ('$stuCode')";
$row=odbc_exec($sqlconn,$sql1);
}
odbc_close($sqlconn);


The Insert into statement seems to work but will not loop through the records.
If I leave it in its current state I get 1 row inserted into the table.
How can I get it to insert all the data in to the table?

Thank you

Re: Inserting variable data into a SQL Server table

Posted: Tue Sep 30, 2008 9:33 am
by papa
You need to loop through the insert statement.

Re: Inserting variable data into a SQL Server table

Posted: Tue Sep 30, 2008 9:43 am
by bendertez
Hi
Do you mean loop through within the SQL statement?
Thanks

Re: Inserting variable data into a SQL Server table

Posted: Tue Sep 30, 2008 9:46 am
by papa
Well you want to insert $stuCode into the table right?

So What you need to do is to include the insert statement in the while loop so you insert the values that is generated.

Re: Inserting variable data into a SQL Server table

Posted: Tue Sep 30, 2008 1:12 pm
by RobertGonzalez
Move your insert code into your while loop code.