Code:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
int main()
{
MYSQL *conn;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *result;
int input_scan;
char *server = "localhost";
char *user = "root";
char *password = "1"; /* set me first */
char *database = "PROJECT";
char query[100];
int i;
int choice;
int system_loop = 1;
while (system_loop != 0)
{
do
{
system("clear");
printf("Welcome to Jason Chea & Myles Hattabaugh Project\n\n");
printf("Please make a choice:\n"
"1) View Complete Inventory \n"
"2) Item Description \n"
"3) Add Game \n"
"4) Remove Item \n"
"5) Exit \n\n");
printf("Please select a option:");
scanf("%d", &choice);
if (choice == 1)
{
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "SELECT * FROM BARCODE_SCAN"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
result = mysql_use_result(conn);
/* output table name */
int num_fields = mysql_num_fields(result);
printf("\nInventory:\n\n");
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
if (i == 0)
{
while(field = mysql_fetch_field(result))
{
printf("%s" " || ", field->name);
}
printf("\n----------------------------------------------------------------------------------\n");
}
printf("%-18s", row[i] ? row[i] : "NULL");
}
printf("\n");
}
if(mysql_query(conn, "SELECT * FROM BARCODE_SCAN"))
{
mysql_error(conn);
}
result = mysql_store_result(conn);
printf("\n\n");
if (result == NULL)
{
mysql_error(conn);
}
exit(0);
}
else if (choice == 2)
{
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
printf("Enter or Scan a UPC Code:");
scanf("%i", &input_scan);
sprintf(query, "SELECT * FROM BARCODE_SCAN WHERE BARCODE_ID LIKE ('%i')", input_scan);
mysql_query(conn,query);
{
mysql_error(conn);
}
result = mysql_store_result(conn);
if (result == NULL)
{
mysql_error(conn);
}
int num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
if (i == 0)
{
while(field = mysql_fetch_field(result))
{
printf("%s" " || ", field->name);
}
printf("\n----------------------------------------------------------------------------------\n");
}
printf("%-18s", row[i] ? row[i] : "NULL");
}
printf("\n");
}
exit(0);
}
else if (choice == 3)
{
char insertQuery [350];
char UPC;
char game_title;
char developer;
char price;
char quantity;
printf("Please enter game criteria:\n");
printf("Enter UPC Code:");
scanf("%s", &UPC);
printf("Enter Game Title:");
scanf("%s", &game_title);
printf("Enter Developer:");
scanf("%s", &developer);
printf("Enter Game Price:");
scanf("%s", &price);
printf("Enter Quantity on Hand:");
scanf("%s", &quantity);
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
sprintf(insertQuery, "INSERT INTO BARCODE_SCAN(BARCODE_ID, GAME_TITLE, PRICE, QUANTITY) VALUES (('%s', '%s', '%s', '%s', '%s')", &UPC, &game_title, &developer, &price, &quantity);
mysql_query(conn,insertQuery);
if(result)
{
mysql_free_result(result);
result = NULL;
}
}
else if (choice == 4)
{
printf("option not available yet");
}
else if (choice == 5)
{
exit(0);
}
else
{
printf("Invalid Choice");
}
}
while (choice !=5);
{
/* close connection */
mysql_free_result(result);
mysql_close(conn);
}
return;
}
}
Okay I'm back. I fixed option two for the time being. I know my indentation may be off a bit (Allman's Style) because of all the elting and fixing I have been doing I have decided to run back through at the end to fix the indentation errors if any.